min-heap-typed 1.50.1 → 1.50.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/data-structures/base/iterable-base.d.ts +120 -9
- package/dist/data-structures/base/iterable-base.js +143 -7
- package/dist/data-structures/binary-tree/avl-tree.d.ts +72 -47
- package/dist/data-structures/binary-tree/avl-tree.js +101 -72
- package/dist/data-structures/binary-tree/binary-indexed-tree.d.ts +22 -0
- package/dist/data-structures/binary-tree/binary-indexed-tree.js +22 -0
- package/dist/data-structures/binary-tree/binary-tree.d.ts +244 -199
- package/dist/data-structures/binary-tree/binary-tree.js +484 -376
- package/dist/data-structures/binary-tree/bst.d.ts +92 -79
- package/dist/data-structures/binary-tree/bst.js +68 -76
- package/dist/data-structures/binary-tree/rb-tree.d.ts +127 -57
- package/dist/data-structures/binary-tree/rb-tree.js +152 -99
- package/dist/data-structures/binary-tree/segment-tree.d.ts +99 -6
- package/dist/data-structures/binary-tree/segment-tree.js +127 -10
- package/dist/data-structures/binary-tree/tree-multimap.d.ts +72 -58
- package/dist/data-structures/binary-tree/tree-multimap.js +102 -85
- package/dist/data-structures/graph/abstract-graph.d.ts +1 -78
- package/dist/data-structures/graph/abstract-graph.js +3 -189
- package/dist/data-structures/graph/directed-graph.d.ts +73 -0
- package/dist/data-structures/graph/directed-graph.js +131 -0
- package/dist/data-structures/graph/map-graph.d.ts +8 -0
- package/dist/data-structures/graph/map-graph.js +14 -0
- package/dist/data-structures/graph/undirected-graph.d.ts +76 -7
- package/dist/data-structures/graph/undirected-graph.js +151 -18
- package/dist/data-structures/hash/hash-map.d.ts +254 -28
- package/dist/data-structures/hash/hash-map.js +347 -78
- package/dist/data-structures/heap/heap.d.ts +95 -25
- package/dist/data-structures/heap/heap.js +95 -26
- package/dist/data-structures/linked-list/doubly-linked-list.d.ts +126 -63
- package/dist/data-structures/linked-list/doubly-linked-list.js +141 -77
- package/dist/data-structures/linked-list/singly-linked-list.d.ts +154 -106
- package/dist/data-structures/linked-list/singly-linked-list.js +164 -115
- package/dist/data-structures/linked-list/skip-linked-list.d.ts +63 -36
- package/dist/data-structures/linked-list/skip-linked-list.js +63 -36
- package/dist/data-structures/matrix/matrix.d.ts +35 -4
- package/dist/data-structures/matrix/matrix.js +50 -11
- package/dist/data-structures/priority-queue/max-priority-queue.d.ts +10 -0
- package/dist/data-structures/priority-queue/max-priority-queue.js +10 -0
- package/dist/data-structures/priority-queue/min-priority-queue.d.ts +11 -0
- package/dist/data-structures/priority-queue/min-priority-queue.js +11 -0
- package/dist/data-structures/priority-queue/priority-queue.d.ts +8 -0
- package/dist/data-structures/priority-queue/priority-queue.js +8 -0
- package/dist/data-structures/queue/deque.d.ts +139 -35
- package/dist/data-structures/queue/deque.js +200 -62
- package/dist/data-structures/queue/queue.d.ts +103 -49
- package/dist/data-structures/queue/queue.js +111 -49
- package/dist/data-structures/stack/stack.d.ts +51 -21
- package/dist/data-structures/stack/stack.js +58 -22
- package/dist/data-structures/tree/tree.d.ts +57 -3
- package/dist/data-structures/tree/tree.js +77 -11
- package/dist/data-structures/trie/trie.d.ts +135 -34
- package/dist/data-structures/trie/trie.js +153 -33
- package/dist/types/data-structures/binary-tree/binary-tree.d.ts +1 -1
- package/dist/types/data-structures/hash/hash-map.d.ts +4 -3
- package/dist/types/utils/utils.d.ts +1 -0
- package/package.json +2 -2
- package/src/data-structures/base/iterable-base.ts +184 -19
- package/src/data-structures/binary-tree/avl-tree.ts +134 -100
- package/src/data-structures/binary-tree/binary-indexed-tree.ts +22 -0
- package/src/data-structures/binary-tree/binary-tree.ts +674 -671
- package/src/data-structures/binary-tree/bst.ts +127 -136
- package/src/data-structures/binary-tree/rb-tree.ts +199 -166
- package/src/data-structures/binary-tree/segment-tree.ts +145 -11
- package/src/data-structures/binary-tree/tree-multimap.ts +138 -115
- package/src/data-structures/graph/abstract-graph.ts +4 -211
- package/src/data-structures/graph/directed-graph.ts +152 -0
- package/src/data-structures/graph/map-graph.ts +15 -0
- package/src/data-structures/graph/undirected-graph.ts +171 -19
- package/src/data-structures/hash/hash-map.ts +389 -96
- package/src/data-structures/heap/heap.ts +97 -26
- package/src/data-structures/linked-list/doubly-linked-list.ts +156 -83
- package/src/data-structures/linked-list/singly-linked-list.ts +174 -120
- package/src/data-structures/linked-list/skip-linked-list.ts +63 -37
- package/src/data-structures/matrix/matrix.ts +52 -12
- package/src/data-structures/priority-queue/max-priority-queue.ts +10 -0
- package/src/data-structures/priority-queue/min-priority-queue.ts +11 -0
- package/src/data-structures/priority-queue/priority-queue.ts +8 -0
- package/src/data-structures/queue/deque.ts +225 -70
- package/src/data-structures/queue/queue.ts +118 -49
- package/src/data-structures/stack/stack.ts +63 -23
- package/src/data-structures/tree/tree.ts +89 -15
- package/src/data-structures/trie/trie.ts +173 -38
- package/src/types/data-structures/binary-tree/binary-tree.ts +1 -1
- package/src/types/data-structures/hash/hash-map.ts +4 -3
- package/src/types/utils/utils.ts +2 -0
|
@@ -9,14 +9,48 @@ class SinglyLinkedListNode {
|
|
|
9
9
|
* will be stored in the node of a linked list.
|
|
10
10
|
*/
|
|
11
11
|
constructor(value) {
|
|
12
|
-
this.
|
|
13
|
-
this.
|
|
12
|
+
this._value = value;
|
|
13
|
+
this._next = undefined;
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* The function returns the value of a protected variable.
|
|
17
|
+
* @returns The value of the variable `_value` is being returned.
|
|
18
|
+
*/
|
|
19
|
+
get value() {
|
|
20
|
+
return this._value;
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* The above function sets the value of a variable.
|
|
24
|
+
* @param {E} value - The parameter "value" is of type E, which means it can be any type.
|
|
25
|
+
*/
|
|
26
|
+
set value(value) {
|
|
27
|
+
this._value = value;
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* The `next` function returns the next node in a singly linked list.
|
|
31
|
+
* @returns The `next` property is being returned. It can be either a `SinglyLinkedListNode<E>`
|
|
32
|
+
* object or `undefined`.
|
|
33
|
+
*/
|
|
34
|
+
get next() {
|
|
35
|
+
return this._next;
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* The "next" property of a SinglyLinkedListNode is set to the provided value.
|
|
39
|
+
* @param {SinglyLinkedListNode<E> | undefined} value - The `value` parameter is of type
|
|
40
|
+
* `SinglyLinkedListNode<E> | undefined`. This means that it can accept either a
|
|
41
|
+
* `SinglyLinkedListNode` object or `undefined` as its value.
|
|
42
|
+
*/
|
|
43
|
+
set next(value) {
|
|
44
|
+
this._next = value;
|
|
14
45
|
}
|
|
15
46
|
}
|
|
16
47
|
exports.SinglyLinkedListNode = SinglyLinkedListNode;
|
|
17
48
|
class SinglyLinkedList extends base_1.IterableElementBase {
|
|
18
49
|
/**
|
|
19
|
-
* The constructor initializes
|
|
50
|
+
* The constructor initializes a new instance of a class with an optional iterable of elements.
|
|
51
|
+
* @param elements - The `elements` parameter is an optional iterable object that contains the
|
|
52
|
+
* initial elements to be added to the instance of the class. If no `elements` are provided, an empty
|
|
53
|
+
* array will be used as the default value.
|
|
20
54
|
*/
|
|
21
55
|
constructor(elements = []) {
|
|
22
56
|
super();
|
|
@@ -26,22 +60,36 @@ class SinglyLinkedList extends base_1.IterableElementBase {
|
|
|
26
60
|
this.push(el);
|
|
27
61
|
}
|
|
28
62
|
}
|
|
63
|
+
/**
|
|
64
|
+
* The `head` function returns the first node of a singly linked list.
|
|
65
|
+
* @returns The method is returning either a SinglyLinkedListNode object or undefined.
|
|
66
|
+
*/
|
|
29
67
|
get head() {
|
|
30
68
|
return this._head;
|
|
31
69
|
}
|
|
70
|
+
/**
|
|
71
|
+
* The `tail` function returns the last node of a singly linked list.
|
|
72
|
+
* @returns The method is returning either a SinglyLinkedListNode object or undefined.
|
|
73
|
+
*/
|
|
32
74
|
get tail() {
|
|
33
75
|
return this._tail;
|
|
34
76
|
}
|
|
77
|
+
/**
|
|
78
|
+
* The function returns the size of an object.
|
|
79
|
+
* @returns The size of the object, which is a number.
|
|
80
|
+
*/
|
|
35
81
|
get size() {
|
|
36
82
|
return this._size;
|
|
37
83
|
}
|
|
38
84
|
/**
|
|
39
|
-
* Time Complexity: O(n)
|
|
40
|
-
* Space Complexity: O(n)
|
|
85
|
+
* Time Complexity: O(n)
|
|
86
|
+
* Space Complexity: O(n)
|
|
87
|
+
* Linear time, where n is the length of the input array, as it performs a loop to push each element into the linked list.
|
|
88
|
+
* Linear space, as it creates a new node for each element in the array.
|
|
41
89
|
*/
|
|
42
90
|
/**
|
|
43
|
-
* Time Complexity: O(n)
|
|
44
|
-
* Space Complexity: O(n)
|
|
91
|
+
* Time Complexity: O(n)
|
|
92
|
+
* Space Complexity: O(n)
|
|
45
93
|
*
|
|
46
94
|
* The `fromArray` function creates a new SinglyLinkedList instance and populates it with the elements from the given
|
|
47
95
|
* array.
|
|
@@ -56,12 +104,14 @@ class SinglyLinkedList extends base_1.IterableElementBase {
|
|
|
56
104
|
return singlyLinkedList;
|
|
57
105
|
}
|
|
58
106
|
/**
|
|
59
|
-
* Time Complexity: O(1)
|
|
60
|
-
* Space Complexity: O(1)
|
|
107
|
+
* Time Complexity: O(1)
|
|
108
|
+
* Space Complexity: O(1)
|
|
109
|
+
* Constant time, as it involves basic pointer adjustments.
|
|
110
|
+
* Constant space, as it only creates a new node.
|
|
61
111
|
*/
|
|
62
112
|
/**
|
|
63
|
-
* Time Complexity: O(1)
|
|
64
|
-
* Space Complexity: O(1)
|
|
113
|
+
* Time Complexity: O(1)
|
|
114
|
+
* Space Complexity: O(1)
|
|
65
115
|
*
|
|
66
116
|
* The `push` function adds a new node with the given value to the end of a singly linked list.
|
|
67
117
|
* @param {E} value - The "value" parameter represents the value that you want to add to the linked list. It can be of
|
|
@@ -81,12 +131,12 @@ class SinglyLinkedList extends base_1.IterableElementBase {
|
|
|
81
131
|
return true;
|
|
82
132
|
}
|
|
83
133
|
/**
|
|
84
|
-
* Time Complexity: O(1)
|
|
85
|
-
* Space Complexity: O(1)
|
|
134
|
+
* Time Complexity: O(1)
|
|
135
|
+
* Space Complexity: O(1)
|
|
86
136
|
*/
|
|
87
137
|
/**
|
|
88
|
-
* Time Complexity: O(1)
|
|
89
|
-
* Space Complexity: O(1)
|
|
138
|
+
* Time Complexity: O(1)
|
|
139
|
+
* Space Complexity: O(1)
|
|
90
140
|
*
|
|
91
141
|
* The `push` function adds a new node with the given value to the end of a singly linked list.
|
|
92
142
|
* @param {E} value - The "value" parameter represents the value that you want to add to the linked list. It can be of
|
|
@@ -96,12 +146,13 @@ class SinglyLinkedList extends base_1.IterableElementBase {
|
|
|
96
146
|
return this.push(value);
|
|
97
147
|
}
|
|
98
148
|
/**
|
|
99
|
-
* Time Complexity: O(n)
|
|
100
|
-
* Space Complexity: O(1)
|
|
149
|
+
* Time Complexity: O(n)
|
|
150
|
+
* Space Complexity: O(1)
|
|
151
|
+
* Linear time in the worst case, as it may need to traverse the list to find the last element.
|
|
101
152
|
*/
|
|
102
153
|
/**
|
|
103
|
-
* Time Complexity: O(n)
|
|
104
|
-
* Space Complexity: O(1)
|
|
154
|
+
* Time Complexity: O(n)
|
|
155
|
+
* Space Complexity: O(1)
|
|
105
156
|
*
|
|
106
157
|
* The `pop()` function removes and returns the value of the last element in a linked list, updating the head and tail
|
|
107
158
|
* pointers accordingly.
|
|
@@ -129,12 +180,12 @@ class SinglyLinkedList extends base_1.IterableElementBase {
|
|
|
129
180
|
return value;
|
|
130
181
|
}
|
|
131
182
|
/**
|
|
132
|
-
* Time Complexity: O(n)
|
|
133
|
-
* Space Complexity: O(1)
|
|
183
|
+
* Time Complexity: O(n)
|
|
184
|
+
* Space Complexity: O(1)
|
|
134
185
|
*/
|
|
135
186
|
/**
|
|
136
|
-
* Time Complexity: O(n)
|
|
137
|
-
* Space Complexity: O(1)
|
|
187
|
+
* Time Complexity: O(n)
|
|
188
|
+
* Space Complexity: O(1)
|
|
138
189
|
*
|
|
139
190
|
* The `pollLast()` function removes and returns the value of the last element in a linked list, updating the head and tail
|
|
140
191
|
* pointers accordingly.
|
|
@@ -145,12 +196,12 @@ class SinglyLinkedList extends base_1.IterableElementBase {
|
|
|
145
196
|
return this.pop();
|
|
146
197
|
}
|
|
147
198
|
/**
|
|
148
|
-
* Time Complexity: O(1)
|
|
149
|
-
* Space Complexity: O(1)
|
|
199
|
+
* Time Complexity: O(1)
|
|
200
|
+
* Space Complexity: O(1)
|
|
150
201
|
*/
|
|
151
202
|
/**
|
|
152
|
-
* Time Complexity: O(1)
|
|
153
|
-
* Space Complexity: O(1)
|
|
203
|
+
* Time Complexity: O(1)
|
|
204
|
+
* Space Complexity: O(1)
|
|
154
205
|
*
|
|
155
206
|
* The `shift()` function removes and returns the value of the first node in a linked list.
|
|
156
207
|
* @returns The value of the node that is being removed from the beginning of the linked list.
|
|
@@ -164,12 +215,12 @@ class SinglyLinkedList extends base_1.IterableElementBase {
|
|
|
164
215
|
return removedNode.value;
|
|
165
216
|
}
|
|
166
217
|
/**
|
|
167
|
-
* Time Complexity: O(1)
|
|
168
|
-
* Space Complexity: O(1)
|
|
218
|
+
* Time Complexity: O(1)
|
|
219
|
+
* Space Complexity: O(1)
|
|
169
220
|
*/
|
|
170
221
|
/**
|
|
171
|
-
* Time Complexity: O(1)
|
|
172
|
-
* Space Complexity: O(1)
|
|
222
|
+
* Time Complexity: O(1)
|
|
223
|
+
* Space Complexity: O(1)
|
|
173
224
|
*
|
|
174
225
|
* The `pollFirst()` function removes and returns the value of the first node in a linked list.
|
|
175
226
|
* @returns The value of the node that is being removed from the beginning of the linked list.
|
|
@@ -178,12 +229,12 @@ class SinglyLinkedList extends base_1.IterableElementBase {
|
|
|
178
229
|
return this.shift();
|
|
179
230
|
}
|
|
180
231
|
/**
|
|
181
|
-
* Time Complexity: O(1)
|
|
182
|
-
* Space Complexity: O(1)
|
|
232
|
+
* Time Complexity: O(1)
|
|
233
|
+
* Space Complexity: O(1)
|
|
183
234
|
*/
|
|
184
235
|
/**
|
|
185
|
-
* Time Complexity: O(1)
|
|
186
|
-
* Space Complexity: O(1)
|
|
236
|
+
* Time Complexity: O(1)
|
|
237
|
+
* Space Complexity: O(1)
|
|
187
238
|
*
|
|
188
239
|
* The unshift function adds a new node with the given value to the beginning of a singly linked list.
|
|
189
240
|
* @param {E} value - The parameter "value" represents the value of the new node that will be added to the beginning of the
|
|
@@ -203,12 +254,12 @@ class SinglyLinkedList extends base_1.IterableElementBase {
|
|
|
203
254
|
return true;
|
|
204
255
|
}
|
|
205
256
|
/**
|
|
206
|
-
* Time Complexity: O(1)
|
|
207
|
-
* Space Complexity: O(1)
|
|
257
|
+
* Time Complexity: O(1)
|
|
258
|
+
* Space Complexity: O(1)
|
|
208
259
|
*/
|
|
209
260
|
/**
|
|
210
|
-
* Time Complexity: O(1)
|
|
211
|
-
* Space Complexity: O(1)
|
|
261
|
+
* Time Complexity: O(1)
|
|
262
|
+
* Space Complexity: O(1)
|
|
212
263
|
*
|
|
213
264
|
* The addFirst function adds a new node with the given value to the beginning of a singly linked list.
|
|
214
265
|
* @param {E} value - The parameter "value" represents the value of the new node that will be added to the beginning of the
|
|
@@ -218,20 +269,21 @@ class SinglyLinkedList extends base_1.IterableElementBase {
|
|
|
218
269
|
return this.unshift(value);
|
|
219
270
|
}
|
|
220
271
|
/**
|
|
221
|
-
* Time Complexity: O(n)
|
|
222
|
-
* Space Complexity: O(1)
|
|
272
|
+
* Time Complexity: O(n)
|
|
273
|
+
* Space Complexity: O(1)
|
|
274
|
+
* Linear time, where n is the index, as it may need to traverse the list to find the desired node.
|
|
223
275
|
*/
|
|
224
276
|
/**
|
|
225
|
-
* Time Complexity: O(n)
|
|
226
|
-
* Space Complexity: O(1)
|
|
277
|
+
* Time Complexity: O(n)
|
|
278
|
+
* Space Complexity: O(1)
|
|
227
279
|
*
|
|
228
|
-
* The function `
|
|
280
|
+
* The function `at` returns the value at a specified index in a linked list, or undefined if the index is out of range.
|
|
229
281
|
* @param {number} index - The index parameter is a number that represents the position of the element we want to
|
|
230
282
|
* retrieve from the list.
|
|
231
|
-
* @returns The method `
|
|
283
|
+
* @returns The method `at(index: number): E | undefined` returns the value at the specified index in the linked list, or
|
|
232
284
|
* `undefined` if the index is out of bounds.
|
|
233
285
|
*/
|
|
234
|
-
|
|
286
|
+
at(index) {
|
|
235
287
|
if (index < 0 || index >= this.size)
|
|
236
288
|
return undefined;
|
|
237
289
|
let current = this.head;
|
|
@@ -241,12 +293,12 @@ class SinglyLinkedList extends base_1.IterableElementBase {
|
|
|
241
293
|
return current.value;
|
|
242
294
|
}
|
|
243
295
|
/**
|
|
244
|
-
* Time Complexity: O(n)
|
|
245
|
-
* Space Complexity: O(1)
|
|
296
|
+
* Time Complexity: O(n)
|
|
297
|
+
* Space Complexity: O(1)
|
|
246
298
|
*/
|
|
247
299
|
/**
|
|
248
|
-
* Time Complexity: O(n)
|
|
249
|
-
* Space Complexity: O(1)
|
|
300
|
+
* Time Complexity: O(n)
|
|
301
|
+
* Space Complexity: O(1)
|
|
250
302
|
*
|
|
251
303
|
* The function `getNodeAt` returns the node at a given index in a singly linked list.
|
|
252
304
|
* @param {number} index - The `index` parameter is a number that represents the position of the node we want to
|
|
@@ -262,12 +314,12 @@ class SinglyLinkedList extends base_1.IterableElementBase {
|
|
|
262
314
|
return current;
|
|
263
315
|
}
|
|
264
316
|
/**
|
|
265
|
-
* Time Complexity: O(n)
|
|
266
|
-
* Space Complexity: O(1)
|
|
317
|
+
* Time Complexity: O(n)
|
|
318
|
+
* Space Complexity: O(1)
|
|
267
319
|
*/
|
|
268
320
|
/**
|
|
269
|
-
* Time Complexity: O(n)
|
|
270
|
-
* Space Complexity: O(1)
|
|
321
|
+
* Time Complexity: O(n)
|
|
322
|
+
* Space Complexity: O(1)
|
|
271
323
|
*
|
|
272
324
|
* The `deleteAt` function removes an element at a specified index from a linked list and returns the removed element.
|
|
273
325
|
* @param {number} index - The index parameter represents the position of the element that needs to be deleted in the
|
|
@@ -293,12 +345,12 @@ class SinglyLinkedList extends base_1.IterableElementBase {
|
|
|
293
345
|
return true;
|
|
294
346
|
}
|
|
295
347
|
/**
|
|
296
|
-
* Time Complexity: O(n)
|
|
297
|
-
* Space Complexity: O(1)
|
|
348
|
+
* Time Complexity: O(n)
|
|
349
|
+
* Space Complexity: O(1)
|
|
298
350
|
*/
|
|
299
351
|
/**
|
|
300
|
-
* Time Complexity: O(n)
|
|
301
|
-
* Space Complexity: O(1)
|
|
352
|
+
* Time Complexity: O(n)
|
|
353
|
+
* Space Complexity: O(1)
|
|
302
354
|
*
|
|
303
355
|
* The delete function removes a node with a specific value from a singly linked list.
|
|
304
356
|
* @param {E | SinglyLinkedListNode<E>} valueOrNode - The `valueOrNode` parameter can accept either a value of type `E`
|
|
@@ -340,12 +392,12 @@ class SinglyLinkedList extends base_1.IterableElementBase {
|
|
|
340
392
|
return false;
|
|
341
393
|
}
|
|
342
394
|
/**
|
|
343
|
-
* Time Complexity: O(n)
|
|
344
|
-
* Space Complexity: O(1)
|
|
395
|
+
* Time Complexity: O(n)
|
|
396
|
+
* Space Complexity: O(1)
|
|
345
397
|
*/
|
|
346
398
|
/**
|
|
347
|
-
* Time Complexity: O(n)
|
|
348
|
-
* Space Complexity: O(1)
|
|
399
|
+
* Time Complexity: O(n)
|
|
400
|
+
* Space Complexity: O(1)
|
|
349
401
|
*
|
|
350
402
|
* The `addAt` function inserts a value at a specified index in a singly linked list.
|
|
351
403
|
* @param {number} index - The index parameter represents the position at which the new value should be inserted in the
|
|
@@ -390,12 +442,14 @@ class SinglyLinkedList extends base_1.IterableElementBase {
|
|
|
390
442
|
this._size = 0;
|
|
391
443
|
}
|
|
392
444
|
/**
|
|
393
|
-
* Time Complexity: O(n)
|
|
394
|
-
* Space Complexity: O(n)
|
|
445
|
+
* Time Complexity: O(n)
|
|
446
|
+
* Space Complexity: O(n)
|
|
447
|
+
* Linear time, where n is the length of the list, as it needs to traverse the entire list to convert it to an array.
|
|
448
|
+
* Linear space, as it creates an array with the same length as the list.
|
|
395
449
|
*/
|
|
396
450
|
/**
|
|
397
|
-
* Time Complexity: O(n)
|
|
398
|
-
* Space Complexity: O(n)
|
|
451
|
+
* Time Complexity: O(n)
|
|
452
|
+
* Space Complexity: O(n)
|
|
399
453
|
*
|
|
400
454
|
* The `toArray` function converts a linked list into an array.
|
|
401
455
|
* @returns The `toArray()` method is returning an array of type `E[]`.
|
|
@@ -410,12 +464,12 @@ class SinglyLinkedList extends base_1.IterableElementBase {
|
|
|
410
464
|
return array;
|
|
411
465
|
}
|
|
412
466
|
/**
|
|
413
|
-
* Time Complexity: O(n)
|
|
414
|
-
* Space Complexity: O(1)
|
|
467
|
+
* Time Complexity: O(n)
|
|
468
|
+
* Space Complexity: O(1)
|
|
415
469
|
*/
|
|
416
470
|
/**
|
|
417
|
-
* Time Complexity: O(n)
|
|
418
|
-
* Space Complexity: O(1)
|
|
471
|
+
* Time Complexity: O(n)
|
|
472
|
+
* Space Complexity: O(1)
|
|
419
473
|
*
|
|
420
474
|
* The `reverse` function reverses the order of the nodes in a singly linked list.
|
|
421
475
|
* @returns The reverse() method does not return anything. It has a return type of void.
|
|
@@ -436,36 +490,12 @@ class SinglyLinkedList extends base_1.IterableElementBase {
|
|
|
436
490
|
return this;
|
|
437
491
|
}
|
|
438
492
|
/**
|
|
439
|
-
* Time Complexity: O(n)
|
|
440
|
-
* Space Complexity: O(1)
|
|
441
|
-
*/
|
|
442
|
-
/**
|
|
443
|
-
* Time Complexity: O(n) - Linear time, where n is the length of the list, as it needs to reverse the pointers of each node.
|
|
444
|
-
* Space Complexity: O(1) - Constant space.
|
|
445
|
-
*
|
|
446
|
-
* The `find` function iterates through a linked list and returns the first element that satisfies a given condition.
|
|
447
|
-
* @param callback - A function that takes a value of type E as its parameter and returns a boolean value. This
|
|
448
|
-
* function is used to determine whether a particular value in the linked list satisfies a certain condition.
|
|
449
|
-
* @returns The method `find` returns the first element in the linked list that satisfies the condition specified by
|
|
450
|
-
* the callback function. If no element satisfies the condition, it returns `undefined`.
|
|
451
|
-
*/
|
|
452
|
-
find(callback) {
|
|
453
|
-
let current = this.head;
|
|
454
|
-
while (current) {
|
|
455
|
-
if (callback(current.value)) {
|
|
456
|
-
return current.value;
|
|
457
|
-
}
|
|
458
|
-
current = current.next;
|
|
459
|
-
}
|
|
460
|
-
return undefined;
|
|
461
|
-
}
|
|
462
|
-
/**
|
|
463
|
-
* Time Complexity: O(n) - Linear time, where n is the length of the list, as it needs to reverse the pointers of each node.
|
|
464
|
-
* Space Complexity: O(1) - Constant space.
|
|
493
|
+
* Time Complexity: O(n)
|
|
494
|
+
* Space Complexity: O(1)
|
|
465
495
|
*/
|
|
466
496
|
/**
|
|
467
|
-
* Time Complexity: O(n)
|
|
468
|
-
* Space Complexity: O(1)
|
|
497
|
+
* Time Complexity: O(n)
|
|
498
|
+
* Space Complexity: O(1)
|
|
469
499
|
*
|
|
470
500
|
* The `indexOf` function returns the index of the first occurrence of a given value in a linked list.
|
|
471
501
|
* @param {E} value - The value parameter is the value that you want to find the index of in the linked list.
|
|
@@ -485,12 +515,12 @@ class SinglyLinkedList extends base_1.IterableElementBase {
|
|
|
485
515
|
return -1;
|
|
486
516
|
}
|
|
487
517
|
/**
|
|
488
|
-
* Time Complexity: O(n)
|
|
489
|
-
* Space Complexity: O(1)
|
|
518
|
+
* Time Complexity: O(n)
|
|
519
|
+
* Space Complexity: O(1)
|
|
490
520
|
*/
|
|
491
521
|
/**
|
|
492
|
-
* Time Complexity: O(n)
|
|
493
|
-
* Space Complexity: O(1)
|
|
522
|
+
* Time Complexity: O(n)
|
|
523
|
+
* Space Complexity: O(1)
|
|
494
524
|
*
|
|
495
525
|
* The function finds a node in a singly linked list by its value and returns the node if found, otherwise returns
|
|
496
526
|
* undefined.
|
|
@@ -509,12 +539,12 @@ class SinglyLinkedList extends base_1.IterableElementBase {
|
|
|
509
539
|
return undefined;
|
|
510
540
|
}
|
|
511
541
|
/**
|
|
512
|
-
* Time Complexity: O(n)
|
|
513
|
-
* Space Complexity: O(1)
|
|
542
|
+
* Time Complexity: O(n)
|
|
543
|
+
* Space Complexity: O(1)
|
|
514
544
|
*/
|
|
515
545
|
/**
|
|
516
|
-
* Time Complexity: O(n)
|
|
517
|
-
* Space Complexity: O(1)
|
|
546
|
+
* Time Complexity: O(n)
|
|
547
|
+
* Space Complexity: O(1)
|
|
518
548
|
*
|
|
519
549
|
* The `addBefore` function inserts a new value before an existing value in a singly linked list.
|
|
520
550
|
* @param {E | SinglyLinkedListNode<E>} existingValueOrNode - The existing value or node that you want to insert the
|
|
@@ -551,12 +581,12 @@ class SinglyLinkedList extends base_1.IterableElementBase {
|
|
|
551
581
|
return false;
|
|
552
582
|
}
|
|
553
583
|
/**
|
|
554
|
-
* Time Complexity: O(n)
|
|
555
|
-
* Space Complexity: O(1)
|
|
584
|
+
* Time Complexity: O(n)
|
|
585
|
+
* Space Complexity: O(1)
|
|
556
586
|
*/
|
|
557
587
|
/**
|
|
558
|
-
* Time Complexity: O(n)
|
|
559
|
-
* Space Complexity: O(1)
|
|
588
|
+
* Time Complexity: O(n)
|
|
589
|
+
* Space Complexity: O(1)
|
|
560
590
|
*
|
|
561
591
|
* The `addAfter` function inserts a new node with a given value after an existing node in a singly linked list.
|
|
562
592
|
* @param {E | SinglyLinkedListNode<E>} existingValueOrNode - The existing value or node in the linked list after which
|
|
@@ -586,12 +616,12 @@ class SinglyLinkedList extends base_1.IterableElementBase {
|
|
|
586
616
|
return false;
|
|
587
617
|
}
|
|
588
618
|
/**
|
|
589
|
-
* Time Complexity: O(n)
|
|
590
|
-
* Space Complexity: O(1)
|
|
619
|
+
* Time Complexity: O(n)
|
|
620
|
+
* Space Complexity: O(1)
|
|
591
621
|
*/
|
|
592
622
|
/**
|
|
593
|
-
* Time Complexity: O(n)
|
|
594
|
-
* Space Complexity: O(1)
|
|
623
|
+
* Time Complexity: O(n)
|
|
624
|
+
* Space Complexity: O(1)
|
|
595
625
|
*
|
|
596
626
|
* The function counts the number of occurrences of a given value in a linked list.
|
|
597
627
|
* @param {E} value - The value parameter is the value that you want to count the occurrences of in the linked list.
|
|
@@ -608,6 +638,22 @@ class SinglyLinkedList extends base_1.IterableElementBase {
|
|
|
608
638
|
}
|
|
609
639
|
return count;
|
|
610
640
|
}
|
|
641
|
+
/**
|
|
642
|
+
* Time Complexity: O(n)
|
|
643
|
+
* Space Complexity: O(n)
|
|
644
|
+
*/
|
|
645
|
+
/**
|
|
646
|
+
* Time Complexity: O(n)
|
|
647
|
+
* Space Complexity: O(n)
|
|
648
|
+
*
|
|
649
|
+
* The `clone` function returns a new instance of the `SinglyLinkedList` class with the same values
|
|
650
|
+
* as the original list.
|
|
651
|
+
* @returns The `clone()` method is returning a new instance of the `SinglyLinkedList` class, which
|
|
652
|
+
* is a clone of the original list.
|
|
653
|
+
*/
|
|
654
|
+
clone() {
|
|
655
|
+
return new SinglyLinkedList(this.values());
|
|
656
|
+
}
|
|
611
657
|
/**
|
|
612
658
|
* Time Complexity: O(n)
|
|
613
659
|
* Space Complexity: O(n)
|
|
@@ -641,7 +687,7 @@ class SinglyLinkedList extends base_1.IterableElementBase {
|
|
|
641
687
|
return filteredList;
|
|
642
688
|
}
|
|
643
689
|
/**
|
|
644
|
-
* Time Complexity: O(n)
|
|
690
|
+
* Time Complexity: O(n)
|
|
645
691
|
* Space Complexity: O(n)
|
|
646
692
|
*/
|
|
647
693
|
/**
|
|
@@ -667,6 +713,9 @@ class SinglyLinkedList extends base_1.IterableElementBase {
|
|
|
667
713
|
}
|
|
668
714
|
return mappedList;
|
|
669
715
|
}
|
|
716
|
+
/**
|
|
717
|
+
* The function `_getIterator` returns an iterable iterator that yields the values of a linked list.
|
|
718
|
+
*/
|
|
670
719
|
*_getIterator() {
|
|
671
720
|
let current = this.head;
|
|
672
721
|
while (current) {
|