Skip to content

Instantly share code, notes, and snippets.

@radicalsauce
Created October 7, 2015 00:43
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 radicalsauce/5ea425ec1473ea816a2d to your computer and use it in GitHub Desktop.
Save radicalsauce/5ea425ec1473ea816a2d to your computer and use it in GitHub Desktop.
JS Linked List
var makeLinkedList = function(){
var list = {};
list.head = null;
list.tail = null;
list.addToTail = function(value){
var newNode = makeNode(value);
if (!list.head) {
list.head = newNode;
}
if (list.tail) {
list.tail.next = newNode;
}
list.tail = newNode;
};
list.removeHead = function(){
var result = list.head.value;
list.head = list.head.next;
return result;
};
list.contains = function(target){
var currentNode = this.head;
while (currentNode) {
if (currentNode.value === target) {
return true;
} else {
currentNode = currentNode.next;
}
}
return false;
};
return list;
};
var makeNode = function(value){
var node = {};
node.value = value;
node.next = null;
return node;
};
/*
* Complexity: What is the time complexity of the above functions?
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment