Here's the basic code
function ListNode(x) {
this.value = x;
this.next = null;
}
function linkedList(arr){
let list = new ListNode(arr[0]);
let selectedNode = list;
for(let i = 1; i < arr.length; i++){
selectedNode.next = new ListNode(arr[i]);
selectedNode = selectedNode.next
}
return list
}
l = [3, 1, 2, 3, 4, 5];
console.log(linkedList(l));
For some reason, it just works. I don't get how the 'list' variable is changing/updating in linkedList(arr) function. I see selectedNode = list, but list never changes. list initializes with the constructor new ListNode(arr[0]) but after that, the only variable that's changing is selectedNode. There isn't even code for list.next to change to something.
So how is it that return list returns the full linked list?