Skip to content

Instantly share code, notes, and snippets.

@tjeastmond
Last active March 18, 2021 19:53
Show Gist options
  • Save tjeastmond/8e9290644e5194e7a39dd749f7454481 to your computer and use it in GitHub Desktop.
Save tjeastmond/8e9290644e5194e7a39dd749f7454481 to your computer and use it in GitHub Desktop.
This was for a simple test on reversing a linked list
function log(msg) {
console.log(msg);
}
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