Im doing remove function, that deletes node from it. Im trying to do recursive to work with any length. And now I stuck with one problem:
a = {value: 1, next: {value: 2, next: {value: 3, next: null}}}
let node = a.next;
node = node.next;
console.log(a)
Why its not rorking? It works only with a.next = a.next.next;
instead of node = node.next
. How to solve this?
CodePudding user response:
You need to log the variable node
instead of a
, Since we mutate node
and not a
a = {value: 1, next: {value: 2, next: {value: 3, next: null}}}
let node = a.next;
node = node.next;
console.log(node)
You can also mutate the next
property inside node
since It's pass by reference.
a = {value: 1, next: {value: 2, next: {value: 3, next: null}}}
let node = a.next;
node.next = node.next.next;
console.log(a)
CodePudding user response:
It's not working because you're reassigning the node variable, but not the next property of the original node.
a = {value: 1, next: {value: 2, next: {value: 3, next: null}}}
let node = a.next;
node.next = node.next.next;
console.log(a)