Created
July 12, 2014 02:49
-
-
Save aessam/6cdb8b3f0a770bed45ec to your computer and use it in GitHub Desktop.
Node minimal reverse Linked List
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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