-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsingly-linked-list.ts
More file actions
197 lines (157 loc) · 3.57 KB
/
Copy pathsingly-linked-list.ts
File metadata and controls
197 lines (157 loc) · 3.57 KB
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
class ListNode {
next: this | null;
value: any;
constructor(value: any) {
this.next = null;
this.value = value;
}
}
class SinglyLinkedList {
head: ListNode | null;
tail: ListNode | null;
length: number;
constructor() {
this.head = null;
this.tail = null;
this.length = 0;
}
push(value: any): this {
const node = new ListNode(value);
if (this.length === 0) {
this.tail = node;
this.head = node;
} else {
this.tail!.next = node;
this.tail = node;
}
this.length += 1;
return this;
}
private findElementBeforeTail(node: ListNode): ListNode {
if (node.next === this.tail) {
return node;
}
return this.findElementBeforeTail(node.next!);
}
shift() {
if (this.length === 0) {
return undefined;
}
const oldHead = this.head;
this.head = this.head!.next;
this.length -= 1;
if (this.length === 0) {
this.tail = null;
}
return oldHead;
}
unshift(value: any) {
const newNode = new ListNode(value);
if (this.length === 0) {
this.tail = newNode;
this.head = newNode;
} else {
newNode.next = this.head;
this.head = newNode;
}
this.length += 1;
return this;
}
pop(): this | undefined {
if (this.length === 1) {
this.tail = null;
this.head = null;
} else if (this.length === 0) {
return undefined;
}
const elemBeforeTail = this.findElementBeforeTail(this.head!);
elemBeforeTail.next = null;
this.tail = elemBeforeTail;
this.length -= 1;
return this;
}
get(index: number) {
if (index < 0 || index > this.length - 1) {
return null;
}
let i = 0;
let node = this.head;
while (i < index) {
node = node!.next;
i++;
}
return node;
}
set(index: number, value: any) {
if (index === undefined || value === undefined) {
return null;
}
const node = this.get(index);
if (!node) {
return null;
}
node.value = value;
return true;
}
insert(index: number, value: any) {
if (index < 0 || index > this.length) {
return false;
}
if (index === 0) {
this.unshift(value);
} else if (index === this.length) {
this.push(value);
} else {
const newNode = new ListNode(value);
const oldNode = this.get(index - 1);
newNode.next = oldNode!.next;
oldNode!.next = newNode;
this.length++;
}
return true;
}
remove(index: number): undefined | boolean | ListNode {
let result: undefined | boolean | ListNode = undefined;
if (index < 0 || index >= this.length) {
return result;
} else if (index === 0) {
result = !!this.shift();
} else if (index === this.length - 1) {
result = !!this.pop();
} else {
let prev = this.get(index - 1);
let removed = prev!.next;
prev!.next = removed!.next;
result = removed!;
}
this.length--;
return result;
}
reverse() {
let i = 0;
let node = this.head;
this.tail = node;
let next: ListNode | null = null;
let prev = null;
while (i < this.length) {
next = node!.next;
node!.next = prev;
prev = node;
node = next;
i++;
}
this.head = prev;
return this;
}
}
export default function singlyLinkedList() {
const myList = new SinglyLinkedList();
myList.push('1');
myList.push('3');
myList.push('5');
myList.push('7');
myList.push('9');
//myList.reverse();
console.log('Removed --', myList.remove(2));
console.log('List --', myList);
};