-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSingleLinkList.js
71 lines (62 loc) · 1.87 KB
/
SingleLinkList.js
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
<script>
function testLinkList(){
function LinkedList(){
this.head = null;
}
LinkedList.prototype.push = function(val){
var node = {
value: val,
next: null
}
if(!this.head){
this.head = node;
}
else{
current = this.head;
while(current.next){
current = current.next;
}
current.next = node;
}
} // push done!
LinkedList.prototype.remove = function(val){
var current = this.head;
//case-1
if(current.value == val){
this.head = current.next;
}
else{
var previous = current;
while(current.next){
//case-3
if(current.value == val){
previous.next = current.next;
break;
}
previous = current;
current = current.next;
}
//case -2
if(current.value == val){
previous.next == null;
}
}
} // remove() done
var sll = new LinkedList();
//push node
sll.push(2);
sll.push(3);
sll.push(4);
//check values by traversing LinkedList
console.log("sll.head:"+sll.head.toSource()); // ({value:2, next:{value:3, next:{value:4, next:null}}})
console.log("sll.head.next:"+sll.head.next.toSource()); //({value:3, next:{value:4, next:null}})
console.log("sll.head.next.next:"+sll.head.next.next.toSource()); //({value:4, next:null})
console.log("pop:"+sll.remove(3));
console.log("[After removed 3] s11.head:"+sll.head.toSource()); // ({value:2, next:{value:4, next:null}})
console.log("[After removed 3] s11.head.next:"+sll.head.next.toSource()); //({value:4, next:null})
console.log("[After removed 3] s11.head.next.next:"+sll.head.next.next); //null
}
function myFunction() {
document.getElementById("demo").innerHTML = testLinkList();
}
</script>