Skip to content

Instantly share code, notes, and snippets.

@jpoechill
Created June 5, 2017 03:57
Show Gist options
  • Save jpoechill/42fc9b1c5cfa0fa823ec58dbdeaa8518 to your computer and use it in GitHub Desktop.
Save jpoechill/42fc9b1c5cfa0fa823ec58dbdeaa8518 to your computer and use it in GitHub Desktop.
Linked Lists with JS
// From ThatJSDude, http://thatjsdude.com/interview/linkedList.html#singlyLinkedList
function linkedList () {
this.head = null
}
linkedList.prototype.push = function (val) {
var node = {
value: val,
next: null
}
if (!this.head) {
this.head = node
} else {
var current = this.head
while (current.next) {
current = current.next
}
current.next = node
}
}
var myLinkedList = new linkedList()
myLinkedList.push("ABCD")
myLinkedList.push("1234")
console.log(myLinkedList.head)
console.log(myLinkedList.head.next)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment