Created
December 11, 2018 06:03
-
-
Save auser/fd578f67a2860232782f77b2c4837844 to your computer and use it in GitHub Desktop.
Just saving this for myself... creating a linked list from a list of nodes.
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 LinkedListNode(v) { | |
this.val = v; | |
this.next = null | |
} | |
LinkedListNode.prototype.print = function() { | |
let node = this; | |
while (node) { | |
process.stdout.write(`${node.val} -> `) | |
node = node.next | |
} | |
console.log('') | |
} | |
const nodes = Array.from(Array(10).keys()).map(i => new LinkedListNode(i)) | |
for (let i = 1; i < nodes.length; i++) { | |
nodes[i - 1].next = nodes[i] | |
} | |
nodes[0].print() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment