-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLinkedList.js
201 lines (167 loc) · 4.11 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
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
class Node {
/**
* @param {any} value
*
* @constructor
*/
constructor(value) {
this.value = value;
this.next = null;
}
}
class LinkedList {
/**
* @constructor
*/
constructor() {
this.head = null;
}
/**
* Checks if the current LinkedList is empty
*
* @returns {boolean}
*/
isEmpty() {
return !(this.head === null);
}
/**
* Removes the Head
*
* @returns {Node}
*/
removeHead() {
this.head = this.head.next;
return this.head;
}
/**
* Prepends a node to the linked list
*
* @param {Node} node
*/
prepend(node) {
const head = this.head;
this.head = node;
node.next = head;
}
/**
* Add's a Node to the linked list
*
* @param {Node} node
*
* @returns {boolean}
*/
add(node) {
if (this.head === null) {
this.head = node;
return;
}
let current = this.head;
while (current.next !== null) {
current = current.next;
}
current.next = node;
return true;
}
/**
* Returns a Node by the given index
*
* @param {number} index
*
* @returns {Node | undefined}
*/
get(index) {
if (index <= -1 || this.head === null) {
return void(0);
}
let current = this.head;
let i = 0;
// (current !== null) until last
// (i < index) until requested index
while ((current !== null) && (i < index)) {
current = current.next;
i++;
}
if (current !== null) {
return current.value;
}
return void(0);
}
/**
* Returns all Nodes with a specific value
*
* @param {any} value
*
* @returns {Array<Node>}
*/
getNodesByValue(value) {
const values = [];
let current = this.head;
let i = 0;
// Iterate over all
while (current !== null) {
if (current.value === value) {
values.push(current);
}
current = current.next;
i++;
}
return values;
}
/**
* Removes a Node by the given index and returns the value of the node
*
* @param {number} index
*
* @returns {any}
*/
remove(index) {
if (index <= -1 || this.head === null) {
throw new RangeError(`Given index (${index}) does not exist.`);
}
// Remove LinkedList Head
if (index === 0) {
const removedValue = this.head.value;
this.head = this.head.next;
return removedValue;
}
let current = this.head;
let previous = null;
while ((current !== null) && (i < index)) { // Iterate over all until the searched index appears
previous = current; // Store previous
current = current.next; // Define new current
i++;
}
if (current !== null) {
// Use the previous.next (current node we want to remove) and set it to current.next (the node after the one we want to remove)
previous.next = current.next;
return current.value; // Return removed data
}
throw new RangeError('Node not found!');
}
/**
* Removes all Nodes by the given value
*
* @param {any} value
*
* @returns {Array<any>}
*/
removeNodesByValue(value) {
const values = [];
let current = this.head;
let previous = null;
// Iterate over all
while (current !== null) {
if (current.value === value) {
values.push(current);
if (current === this.head) {
this.head = this.head.next;
} else {
previous.next = current.next;
}
}
previous = current;
current = current.next;
}
return values;
}
}