Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

node = node.next doesnt work at linked list

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading