Created
October 7, 2015 00:43
-
-
Save radicalsauce/5ea425ec1473ea816a2d to your computer and use it in GitHub Desktop.
JS Linked List
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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