Created
June 5, 2017 03:57
-
-
Save jpoechill/42fc9b1c5cfa0fa823ec58dbdeaa8518 to your computer and use it in GitHub Desktop.
Linked Lists with JS
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
// 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