Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save deepiks/5d5724ca6d2498ee3abd to your computer and use it in GitHub Desktop.

Select an option

Save deepiks/5d5724ca6d2498ee3abd to your computer and use it in GitHub Desktop.
Javascript Linked List - Object literal syntax
var LinkedList = {
_head: null,
_size: null,
createNode: function(d) {
return {
data: d,
next: null
};
},
addToTail: function(d) {
var current,
newNode = this.createNode(d);
if(this._head == null) {
this._head = newNode;
}
else {
current = this._head;
while(current.next != null) {
current = current.next;
}
current.next = newNode;
}
this._size++;
},
removeNode: function(d) {
if(this._head.data == d) {
this._head = this._head.next;
return;
}
var prev = this._head,
current = this._head.next;
while(current.data !=null) {
if(current.data == d) {
prev.next =current.next;
this._size--;
return;
}
prev = current;
current = current.next;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment