-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathLinkedList.js
141 lines (132 loc) · 2.98 KB
/
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
class Node {
constructor(data) {
this.data = data;
this.next = null;
}
}
class LinkedList {
constructor(data) {
this.head = null;
this.tail = null;
this.size = 0;
}
// Adding a node at the beginning of the linkedlist.
prepend(data) {
const node = new Node(data);
if (this.size === 0) {
this.head = node;
this.tail = node;
} else {
node.next = this.head;
this.head = node;
}
this.size++;
}
// Adding a node at the back of the list.
append(data) {
const node = new Node(data);
if (this.size === 0) {
this.head = node;
this.tail = node;
} else {
const temp = this.tail;
this.tail = node;
temp.next = this.tail;
}
this.size++;
}
// Printing the linkedlist
printList() {
let data = "";
let current = this.head;
while (current !== null) {
data = data + current.data + " ";
current = current.next;
}
return data;
}
// Removing first node from the list.
removeFirstElement() {
if (this.size === 0) return null;
let data = this.head.data;
if (this.size === 1) {
this.head = null;
this.tail = null;
} else {
this.head = this.head.next;
}
this.size--;
return data;
}
// Removing last node from the list.
removeLastElement() {
if (this.size === 0) return null;
let data = this.tail.data;
if (this.size === 1) {
this.head = null;
this.tail = null;
} else {
let current = this.head;
while (current.next.next !== null) {
current = current.next;
}
current.next = null;
this.tail = current;
}
this.size--;
return data;
}
// Inserting a node at certain position.
insertAt(pos, data) {
if (pos < 0 || pos > this.size) retun;
if (pos === 0) {
this.prepend(data);
} else if (pos === this.size) {
this.append(data);
} else {
const node = new Node(data);
let prev = null;
let current = this.head;
let counter = 0;
while (counter < pos) {
prev = current;
current = current.next;
counter++;
}
node.next = current;
prev.next = node;
this.size++;
}
}
// Removing a node from certain position.
removeAt(pos) {
if (pos < 0 || pos >= this.size) return null;
if (pos === 0) {
return this.removeFirstElement();
} else if (pos === this.size - 1) {
return this.removeLastElement();
} else {
let prev = null;
let current = this.head;
let counter = 0;
while (counter < pos) {
prev = current;
current = current.next;
counter++;
}
prev.next = current.next;
this.size--;
return current.data;
}
}
}
const list = new LinkedList();
list.prepend(10);
list.prepend(20);
list.append(30);
list.append(40);
console.log(list.printList());
list.insertAt(2, 60);
console.log(list.printList());
list.removeAt(3);
console.log(list.printList());