Last active
March 18, 2021 19:53
-
-
Save tjeastmond/8e9290644e5194e7a39dd749f7454481 to your computer and use it in GitHub Desktop.
This was for a simple test on reversing a linked list
This file contains 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
function log(msg) { | |
console.log(msg); | |
} |
This file contains 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
function Node(val) { | |
this.val = val; | |
this.next = null; | |
} | |
const Nodes = new Node("a"); | |
Nodes.next = new Node("b"); | |
Nodes.next.next = new Node("c"); | |
const reverseList = list => { | |
if (!list) return list; | |
let node = list; | |
let prev = null; | |
while (node) { | |
let tmp = node.next; | |
node.next = prev; | |
prev = node; | |
node = tmp; | |
} | |
return prev; | |
}; | |
console.log("Nodes: ", Nodes); | |
console.log("reverseList(Nodes): ", reverseList(Nodes)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment