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.