0

So Im working on a leetcode problem: https://leetcode.com/problems/merge-two-sorted-lists/ specifically dealing with linkedlists. Now there is TONS of information on linkedlist available so im gonna focus on this specific leetcode problem and it being javascript.

So normally with LL's you have a node represented by an object/class. Im sort of "rusty" on JS so bear with me. Im used to Ruby and C++ so how "objects" are represented in memory in JS is something im still kinda getting re-used to.

Right now this JS question has the linked list represented by a function like this:

/**
 * Definition for singly-linked list.
 * function ListNode(val, next) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.next = (next===undefined ? null : next)
 * }
 */
/**
 * @param {ListNode} list1
 * @param {ListNode} list2
 * @return {ListNode}
 */

So I understand the solution and and everything...but I guess what im struggling with is how this function how no constructor so "this" would represent the DOM object (window) when im thinking in terms of using JS normally (doing automation)...so I guess can someone clear up for me how exactly this works?

Is there an object called ListNode or something in memory like a normal JS object or what? In C++ or Ruby it makes sense because we literally create an object (in ruby, everythings an object! Wheee!) so I can make sense of it in my head....or heck even using a class makes more sense because we have an instance of an object.

But for some reason this is confusing to me. How is this "object" represented when it's just a function?

ANOTHER confusing thing was how when I would print ".next" of a node of these lists, it would actually print the "rest" of the array of numbers....like if I said list2.next (at any point during the traversal of list2) it would print the rest of list2's numbers starting with where I was at. So that made things even more confusing at how things are "actually" being represented.

msmith1114
  • 2,717
  • 3
  • 33
  • 84
  • "*how this function how no constructor*" - this function **is** a constructor. You use `new ListNode(…)` to create an instance. – Bergi Oct 08 '22 at 03:41
  • "*ANOTHER confusing thing was …*" - you might want to [ask a separate question](https://stackoverflow.com/questions/ask) on that. You'll need to share the code of how you construct the list, how you print it, and the exact output you got; we can't help you without those. – Bergi Oct 08 '22 at 03:44

1 Answers1

1

Because the function references this and assigns to properties of it, I think it's safe to say that it's called as a constructor. For example:

function ListNode(val, next) {
  this.val = (val === undefined ? 0 : val)
  this.next = (next === undefined ? null : next)
}
const node1 = new ListNode(5);
const node2 = new ListNode(3, node1);
console.log(node2);

When a function is called with new, the this inside it references the instance being created (an object whose internal prototype is the .prototype of the function), rather than the globalThis / undefined in strict mode. (There's nothing on the prototype here.)

The implementation of the function doesn't look relevant here though, because you don't ever call it, you only use the objects - and the only thing you care about on the objects is the two properties they have, so from your perspective, it could also be like

const node1 = { val: 5, next: null };
const node2 = { val: 3, next: node1 };
console.log(node2);
CertainPerformance
  • 356,069
  • 52
  • 309
  • 320