-
Notifications
You must be signed in to change notification settings - Fork 0
/
linkedlist.js
52 lines (45 loc) · 836 Bytes
/
linkedlist.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
class Node {
constructor(data)
{
this.data = data;
this.next = null;
}
}
class LinkedList {
constructor(){
this.head = null;
}
printList()
{
let nodes = [];
let thead = this.head;
while(thead != null)
{
nodes.push(thead.data);
thead = thead.next;
}
console.log(nodes);
}
push(data)
{
let newNode = new Node(data);
if(this.head == null){
this.head = newNode;
}
else{
let thead = this.head;
while(thead.next != null)
{
thead = thead.next;
}
thead.next = newNode;
}
}
}
let list = new LinkedList();
list.push(2);
list.push(4);
list.push(6);
list.push(8);
list.push(10);
list.printList();