-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlinkedList.js
More file actions
52 lines (46 loc) · 914 Bytes
/
Copy pathlinkedList.js
File metadata and controls
52 lines (46 loc) · 914 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
/*
Implement a linked-list.
*/
var LinkedList = function (initialValue) {
if(!initialValue){
this.head = null
this.tail = null
return;
}
this.tail = createNode(initialValue);
this.head = this.tail;
};
LinkedList.prototype.addToTail = function(value){
var temp = createNode(value)
if(this.tail === null){
this.tail = temp
this.head = temp
}
else{
this.tail.next = temp;
this.tail = temp;
}
}
LinkedList.prototype.removeHead = function(){
if(this.head === this.tail){
this.head = null
this.tail = null
}else{
this.head = this.head.next;
}
}
LinkedList.prototype.contains = function(value, node){
if(node === undefined){
node = this.head;
}
if(node.value === value){
return true;
}
if(!node.next){
return false;
}
return this.contains(value, node.next)
}
function createNode(value){
return {value: value, next: null};
};