-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsingle_list.js
More file actions
41 lines (34 loc) · 732 Bytes
/
Copy pathsingle_list.js
File metadata and controls
41 lines (34 loc) · 732 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
class Node {
constructor(data, next = null) {
this.data = data
this.next = next
}
}
class LinkList{
constructor() {
this.head = null;
this.size = 0;
}
insertHead(data) {
this.head = new Node(data, this.head);
}
}
// Create node
let node1 = new Node(10);
let node2 = new Node(15);
let node3 = new Node(25);
let node4 = new Node(35);
let node5 = new Node(45);
let node6 = new Node(50);
// Connect nodes
node1.next = node2
node2.next = node3
node3.next = node4
node4.next = node5
node5.next = node6
// Insert new node in head
const link_list = new LinkList();
link_list.insertHead(77);
link_list.insertHead(88);
link_list.insertHead(99);
console.table(link_list);