Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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