Created
February 7, 2018 19:51
-
-
Save ddallala/880181b77c8255294bc31a5e768e6217 to your computer and use it in GitHub Desktop.
JS Bin // source https://jsbin.com/nicegon
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
| <!DOCTYPE html> | |
| <html> | |
| <head> | |
| <meta charset="utf-8"> | |
| <meta name="viewport" content="width=device-width"> | |
| <title>JS Bin</title> | |
| </head> | |
| <body> | |
| <script id="jsbin-javascript"> | |
| // Reverse a LinkedList | |
| function LinkedNode(val){ | |
| this.value = val; | |
| this.next = null; | |
| } | |
| LinkedNode.prototype.addNode = function(n){ | |
| if(this.next == null) this.next = n; | |
| else this.next.addNode(n); | |
| return this; | |
| } | |
| LinkedNode.prototype.add = function(val){ | |
| return this.addNode(new LinkedNode(val)); | |
| } | |
| function reverseLinkedList(node,prev){ | |
| var head; | |
| // stop criteria when at last node | |
| if(node.next == null){ | |
| head = node; | |
| if(prev !== undefined) node.next = prev; // if there is only one node in the list | |
| } | |
| // keep track of current node and reverse the rest of the linkedlist | |
| else { | |
| head = reverseLinkedList(node.next,node); | |
| node.next = prev; // once flip of sub liknedlist done, then point to prev | |
| } | |
| return head; | |
| } | |
| var ll = new LinkedNode(5); | |
| console.log(ll.add(4).add(3).add(2).add(1)) | |
| //console.log(reverseLinkedList(ll)) | |
| </script> | |
| <script id="jsbin-source-javascript" type="text/javascript">// Reverse a LinkedList | |
| function LinkedNode(val){ | |
| this.value = val; | |
| this.next = null; | |
| } | |
| LinkedNode.prototype.addNode = function(n){ | |
| if(this.next == null) this.next = n; | |
| else this.next.addNode(n); | |
| return this; | |
| } | |
| LinkedNode.prototype.add = function(val){ | |
| return this.addNode(new LinkedNode(val)); | |
| } | |
| function reverseLinkedList(node,prev){ | |
| var head; | |
| // stop criteria when at last node | |
| if(node.next == null){ | |
| head = node; | |
| if(prev !== undefined) node.next = prev; // if there is only one node in the list | |
| } | |
| // keep track of current node and reverse the rest of the linkedlist | |
| else { | |
| head = reverseLinkedList(node.next,node); | |
| node.next = prev; // once flip of sub liknedlist done, then point to prev | |
| } | |
| return head; | |
| } | |
| var ll = new LinkedNode(5); | |
| console.log(ll.add(4).add(3).add(2).add(1)) | |
| //console.log(reverseLinkedList(ll))</script></body> | |
| </html> |
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
| // Reverse a LinkedList | |
| function LinkedNode(val){ | |
| this.value = val; | |
| this.next = null; | |
| } | |
| LinkedNode.prototype.addNode = function(n){ | |
| if(this.next == null) this.next = n; | |
| else this.next.addNode(n); | |
| return this; | |
| } | |
| LinkedNode.prototype.add = function(val){ | |
| return this.addNode(new LinkedNode(val)); | |
| } | |
| function reverseLinkedList(node,prev){ | |
| var head; | |
| // stop criteria when at last node | |
| if(node.next == null){ | |
| head = node; | |
| if(prev !== undefined) node.next = prev; // if there is only one node in the list | |
| } | |
| // keep track of current node and reverse the rest of the linkedlist | |
| else { | |
| head = reverseLinkedList(node.next,node); | |
| node.next = prev; // once flip of sub liknedlist done, then point to prev | |
| } | |
| return head; | |
| } | |
| var ll = new LinkedNode(5); | |
| console.log(ll.add(4).add(3).add(2).add(1)) | |
| //console.log(reverseLinkedList(ll)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment