Skip to content

Instantly share code, notes, and snippets.

@sushiljainam
Last active April 5, 2017 20:30
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 sushiljainam/f77d5314a8a4878192125432d49af6e1 to your computer and use it in GitHub Desktop.
Save sushiljainam/f77d5314a8a4878192125432d49af6e1 to your computer and use it in GitHub Desktop.
linked list using JS, simple
var node = function (value){
this.value = value;
this.next = null;
return this;
}
var list = function (){
this.head = null;
return this;
}
list.prototype.last = function (){
var c = this.head;
while(c&&c.next){
c = c.next;
}
return c;
};
list.prototype.push = function(value){
var n = new node(value);
if(!!this.last()) {
this.last().next = n;
} else {
this.head = n;
}
return this;
};
list.prototype.print = function (){
var c = this.head;
while(c){
console.log(c.value);
c = c.next;
}
return true;
};
list.prototype.length = function (){
var l = 0,c = this.head;
while(c){
c = c.next;l++;
}
return l;
};
//---- TEST ----
ll.push(5).push(7);
ll.push(4);
ll.last();
ll.print();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment