forked from hitesh00025/Algorithms-In-Javascript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlinkedlist.js
More file actions
32 lines (28 loc) · 643 Bytes
/
Copy pathlinkedlist.js
File metadata and controls
32 lines (28 loc) · 643 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
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;
}
}
var sll = new LinkedList();
//push node
sll.push(2);
sll.push(3);
sll.push(4);
//check values by traversing LinkedList
console.log(sll.head); //Object {data: 2, next: Object}
console.log(sll.head.next); //Object {data: 4, next: null}
console.log(sll.head.next.next); //Object {data: 4, next: null}