I'm trying to understand Javascript's pointer by implementing my own LinkedList. The ListNode
class looks like this:
function ListNode(val) {
this.val = val;
this.next = null;
}
I then have a function to create a LinkedList with numbers from 0 to 100:
function fillList() {
let output = new ListNode();
let curr = output;
for(let i = 0; i < 100; i++) {
if(curr) {
curr.val = i;
} else {
curr = new ListNode(i);
}
curr = curr.next;
}
return output;
}
The problem is after the return
, output
has nothing but 0
as its value. This means that the for
loop doesn't work, especially when move curr
to its curr.next
and assign a ListNode
to it.
The logic seems to be fine for me, what goes wrong?