-
Notifications
You must be signed in to change notification settings - Fork 3
/
SinglayLinkedlist.js
220 lines (162 loc) · 3.47 KB
/
SinglayLinkedlist.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
class ListNode {
constructor(val) {
this.val = val;
this.next = null;
}
}
class SingleLinkedList {
constructor(array = []) {
this.head = null;
this.tail = null;
this.length = 0;
if (Array.isArray(array)) {
array.forEach((el) => {
this.push(el);
});
}
}
push(val) {
let newNode = new ListNode(val);
if (this.head === null) {
this.head = newNode;
this.tail = this.head;
} else {
this.tail.next = newNode;
this.tail = newNode;
}
this.length++;
return this;
}
unshift(val) {
let newNode = new ListNode(val);
if (this.head === null) {
this.head = newNode;
this.tail = this.head;
} else {
newNode.next = this.head;
this.head = newNode;
}
this.length++;
return this;
}
insert(index, val) {
if (index < 0 || index > this.length) {
return undefined;
}
if (index === 0) {
return this.unshift(val).length;
}
if (index === this.length) {
return this.push(val).length;
}
let newNode = new ListNode(val);
let currentNode = this.head;
let counter = 0;
while (currentNode) {
if (counter === index - 1) {
break;
}
counter++;
currentNode = currentNode.next;
}
newNode.next = currentNode.next;
currentNode.next = newNode;
this.length++;
return this.length;
}
getNode(index) {
if (index < 0 || index > this.length) {
return undefined;
}
let currentNode = this.head;
let counter = 0;
while (currentNode) {
if (counter === index) {
break;
}
counter++;
currentNode = currentNode.next;
}
return currentNode;
}
get(index) {
let node = this.getNode(index);
return node ? node.val : null;
}
set(index, val) {
let node = this.getNode(index);
if (node) {
node.val = val;
return true;
}
return false;
}
shift() {
if (!this.head) {
return undefined;
}
let temp = this.head;
this.head = this.head.next;
temp.next = null;
this.length--;
return temp;
}
remove(index) {
if (index < 0 || index >= this.length) {
return undefined;
}
if (index === 0) {
return this.shift();
}
if (index === this.length) {
return this.pop();
}
let removedNode = null;
if (this.length === 1) {
removedNode = this.shift();
} else {
let previousNode = this.getNode(index - 1);
removedNode = previousNode.next;
previousNode.next = previousNode.next.next;
removedNode.next = null;
}
this.length--;
return removedNode;
}
pop() {
if (!this.tail) {
return undefined;
}
let previousNode = this.getNode(this.length - 2);
let deletedNode = previousNode.next;
previousNode.next = deletedNode.next;
deletedNode.next = null;
this.tail = previousNode;
this.length--;
return deletedNode;
}
reverse() {
let node = this.head;
let previousNode = null;
this.tail = this.head;
while (node) {
let tempNode = node.next;
node.next = previousNode;
previousNode = node;
node = tempNode;
}
this.head = previousNode;
}
}
const list = new SingleLinkedList();
list.push(1);
list.push(2);
list.push(3);
list.insert(0, 4);
list.insert(4, 4);
list.shift();
list.unshift(100);
list.pop();
list.remove(0);
list.remove(2);
console.log(JSON.stringify(list, null, 4));