Skip to content

Instantly share code, notes, and snippets.

@auser
Created December 11, 2018 06:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save auser/fd578f67a2860232782f77b2c4837844 to your computer and use it in GitHub Desktop.
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.
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