-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathLinkedList.js
More file actions
54 lines (45 loc) · 1.16 KB
/
Copy pathLinkedList.js
File metadata and controls
54 lines (45 loc) · 1.16 KB
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
53
54
var LinkedList = function(e){
var that = {}, first, last;
that.length = 0;
that.push = function(value){
var node = new Node(value);
if(first == null){
first = last = node;
}else{
last.next = node;
last = node;
}
++that.length;
};
that.pop = function(){
var value = first;
first = first.next;
--that.length;
return value;
};
that.remove = function(index) {
var i = 0;
var current = first, previous;
if(index === 0){
//handle special case - first node
first = current.next;
}else{
while(i++ < index){
//set previous to first node
previous = current;
//set current to the next one
current = current.next
}
//skip to the next node
previous.next = current.next;
}
--that.length;
return current.value;
};
var Node = function(value){
this.value = value;
var next = {};
};
return that;
};
exports.LinkedList = LinkedList;