-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlinkedList.ts
212 lines (169 loc) · 4.81 KB
/
linkedList.ts
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
class NodeItem {
value: string | number | null;
next: NodeItem | null;
constructor(value: NodeItem['value']) {
this.value = value;
this.next = null;
}
}
export default class LinkedList {
head: NodeItem | null;
tail: NodeItem | null;
length: number;
constructor() {
this.head = null;
this.tail = null;
this.length = 0;
}
/**
* Add a new node to the end of the list. The newly added node becomes the tail
* @param value the value to be added to the list
*/
push(value: NodeItem['value']): LinkedList {
let newNode = new NodeItem(value);
if (this.tail) {
this.tail.next = newNode;
this.tail = newNode;
} else {
this.head = newNode;
this.tail = this.head;
}
this.length++;
return this;
}
/**
* Removes the last node (tail) from the list
*/
pop(): NodeItem | undefined {
if (!this.head) return undefined;
let currentNode = this.head;
let newTail = currentNode;
while (currentNode.next) {
newTail = currentNode;
currentNode = currentNode.next;
}
newTail.next = null;
this.tail = newTail;
this.length--;
if (this.length === 0) {
this.head = null;
this.tail = null;
}
return currentNode;
}
/**
* Removes the first node (head) from the list
*/
shift(): NodeItem | undefined {
if (!this.head) return undefined;
let currentHead = this.head;
this.head = currentHead.next;
this.length--;
if (this.length === 0) {
this.tail = this.head;
}
return currentHead;
}
/**
* Add a new node item to the start of the list. The newly added node becomes the haed
* @param value the value to be added to the list
*/
unshift(value: NodeItem['value']): LinkedList {
let newNode = new NodeItem(value);
newNode.next = this.head;
this.head = newNode;
if (!this.tail) {
this.tail = this.head;
}
this.length++;
return this;
}
/**
* Return the node at the given position in the list
* @param index the position of the node in the list
*/
get(index: number): NodeItem | null {
if (index > this.length) return null;
if (index < 0) return null;
if (!this.head) return null;
let currentNode: NodeItem | null = this.head;
let currentIndex = 0;
do {
if (currentIndex === index) {
return currentNode;
} else {
currentIndex++;
currentNode = currentNode?.next;
}
} while (currentNode?.next && index < this.length);
return currentNode;
}
/**
* Overrides a new at a given location in the list. Time Complexity: O(N)
* @param index the location of the item in the list
* @param value the value of the new node
*/
set(index: number, value: NodeItem['value']): boolean {
let foundNode = this.get(index);
if (foundNode) {
foundNode.value = value;
return true;
} else {
return false;
}
}
/**
* Adds a new value to the list in the specified position. Time Complexity: O(N)
* @param index the position to add the value
* @param value the value to be added
*/
insert(index: number, value: NodeItem['value']): boolean {
if (index < 0) return false;
if (index > this.length) return false;
if (index === 0) {
return !!this.unshift(value);
}
if (index === this.length) {
return !!this.push(value);
}
let newNode = new NodeItem(value);
let nodeAtPreviousIndex = this.get(index - 1) as NodeItem; // we're certain the node exists at this point
newNode.next = nodeAtPreviousIndex?.next ?? null;
nodeAtPreviousIndex.next = newNode;
return true;
}
/**
* Deletes a node in the specified position from the list. Time Complexity: O(N)
* @param index the position of the item in the list
*/
remove(index: number): NodeItem | undefined {
if (index < 0) return undefined;
if (index > this.length - 1) return undefined;
if (!this.head) return undefined;
if (index === 0) return this.shift();
if (index === this.length - 1) return this.pop();
let returnedNode = this.get(index - 1) as NodeItem; // we're certain the node exists at this point
let removedItem = returnedNode.next;
returnedNode.next = removedItem?.next ?? null;
return removedItem ?? undefined;
}
/**
* Inverts the order of the items in the list, in place, without creating a new list
*/
reverse(): LinkedList {
if (this.length < 2) return this;
let previousNode = this.head;
let currentNode = this.head?.next;
let nextNode = currentNode?.next;
while (currentNode) {
nextNode = currentNode.next;
currentNode.next = previousNode;
previousNode = currentNode;
currentNode = nextNode;
}
let copyOfhead = this.head;
this.head = this.tail;
this.tail = copyOfhead;
return this;
}
}