Skip to content

Instantly share code, notes, and snippets.

@aessam
Created July 12, 2014 02:49
Show Gist options
  • Select an option

  • Save aessam/6cdb8b3f0a770bed45ec to your computer and use it in GitHub Desktop.

Select an option

Save aessam/6cdb8b3f0a770bed45ec to your computer and use it in GitHub Desktop.
Node minimal reverse Linked List
targetList = {n:{n:{n:{v:1},v:2},v:3},v:4};
function reverseLinkedList(n){
if(n.n){
v = reverseLinkedList(n.n); // simply get the child until you hit the end.
v.n=n; // in the new Order parent node is the child of its child.
}else{
delete targetList.n; // without this line the list will be circuler
targetList = n; // replace the head with the tail
}
return n;
}
function printList(l){
var res = "";
if(l.n)
res = printList(l.n) + " ";
return res + l.v;;
}
console.log("Before \t" + printList(targetList));
reverseLinkedList(targetList);
console.log("After \t" + printList(targetList));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment