Skip to content

Instantly share code, notes, and snippets.

@im4aLL
Created April 10, 2017 07:10
Show Gist options
  • Save im4aLL/a142fd2dafd3663cd937bef82579e1e6 to your computer and use it in GitHub Desktop.
Save im4aLL/a142fd2dafd3663cd937bef82579e1e6 to your computer and use it in GitHub Desktop.
LinkedList Javascript
function LinkedList() {
this.list = null;
this.length = 0;
}
LinkedList.prototype.add = function(value) {
if( this.list === null ) {
this.list = this._node(value);
}
else {
var currentNode = this.list;
while(currentNode.next !== null) {
currentNode = currentNode.next;
}
currentNode.next = this._node(value);
}
this.length++;
return this;
}
LinkedList.prototype._node = function(value) {
return {
value: value,
next: null
}
}
LinkedList.prototype.toString = function(node) {
if(!node) {
node = this.list;
}
console.log(node.value);
if(node.next !== null) {
this.toString(node.next);
}
}
var ll = new LinkedList();
ll.add(10).add(20).add(30).add('hola').toString();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment