I'm trying to reverse a linked list using recursion. Everything goes well until the end and I end up getting broken result.
Can someone tell me what I'm doing wrong?
const reverseLinkedList = (node, newChildOldParent=null ) => {
if( node.next ){
reverseLinkedList(node.next, node);
}
node.next = newChildOldParent;
return node;
}
const someList = {
value: 1,
next: {
value: 2,
next: {
value: 3,
next: {
value: 4,
next: null
}
}
}
};
console.log(reverseLinkedList( someList ))
I get
{ value: 1, next: null }
Instead of the reversed linked List.
Where am I going wrong?
debugger;too @FrankerZ :)console.log()is forevernextnode.