-
Notifications
You must be signed in to change notification settings - Fork 2
/
singlyLinkedList.ts
201 lines (165 loc) · 3.97 KB
/
singlyLinkedList.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
import { LinkedListApi, NodeType, DataType, Node } from "./helper.ts";
export class SinglyLinkedList implements LinkedListApi {
private head: null|NodeType
private tail: null|NodeType
public size: number
constructor() {
this.head = null
this.tail = null
this.size = 0
}
// insert in the head
prepend(data: DataType<any>) {
if (this.head === null) {
const newNode = new Node(data);
this.head = newNode
this.tail = newNode
return true;
}
const currentNode = this.head
this.head = new Node(data)
this.head.next = currentNode
if (currentNode.next === null) {
this.tail = currentNode
}
this.size++
return true;
}
// inset in the tail
append(data: DataType<any>) {
if (this.head === null) {
const newNode = new Node(data);
this.head = newNode
this.tail = newNode
return true;
}
let currentNode: NodeType = this.head
while (currentNode !== null) {
const newItem = new Node(data);
this.tail = newItem
if (currentNode.next === null) {
currentNode.next = newItem
this.size++;
return true;
}
currentNode = currentNode.next
}
return false;
}
// // add anywhere of the linked list except head & tail
add(data: DataType<any>, position: number) {
if (position === 0) {
console.error("Use `prepend()` method to insert data at Head.");
return false;
} else if (position === this.size) {
console.error("Use `append()` method to insert data at Tail.");
return false;
} else if (position > this.size && position != this.size + 1) {
console.error(`Out of range. Size of the linkedList is ${this.size}`);
return false;
}
let prevNode: NodeType = null;
let currentNode = this.head
let count = 0
while (currentNode !== null) {
if (count === position) {
prevNode!.next = new Node(data)
prevNode!.next.next = currentNode
this.size++
}
prevNode = currentNode
currentNode = currentNode.next
count++
}
return true;
}
getFromHead() {
if (this.head === null) {
return false
}
return this.head.data;
}
getFromTail() {
if (this.tail === null) {
return false
}
return this.tail.data
}
print() {
let currentNode = this.head
while (currentNode !== null) {
console.log(currentNode.data);
currentNode = currentNode.next
}
}
remove(key: string|number) {
if (this.head === null) {
return false;
} else if (this.head!.next === null) {
this.tail = null
}
if (this.head.data.key === key) {
const prevHeadData = this.head.data
this.head = this.head.next
if (this.head!.next === null) this.tail = this.head
this.size--
if (this.head === null) this.size = 0
return prevHeadData;
}
// two pointer approach
let previousNode: NodeType = null;
let currentNode: NodeType = this.head
while (currentNode !== null) {
if (key === currentNode.data.key) {
const temp = currentNode.data
previousNode!.next = currentNode.next
if (currentNode!.next === null) this.tail = previousNode
this.size--
return temp;
}
previousNode = currentNode
currentNode = currentNode.next
}
return false;
}
update(key: string|number, newValue: any) {
if (this.head === null) {
return false;
}
if (this.head.data.key === key) {
this.head.data.value = newValue
return this.head.data;
}
if (this.tail!.data.key === key) {
this.tail!.data.value = newValue
}
let currentNode = this.head.next
while (currentNode !== null) {
if (currentNode.data.key === key) {
currentNode.data.value = newValue
return currentNode.data;
}
currentNode = currentNode.next
}
return false;
}
search(key: string|number) {
if (this.head === null) {
return null;
}
if (this.head.data.key === key) {
return this.head.data;
}
if (this.tail!.data.key === key) {
return this.tail!.data
}
let currentNode = this.head.next
while (currentNode !== null) {
if (currentNode.data.key === key) {
return currentNode.data;
}
currentNode = currentNode.next
}
return null;
}
}