-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path707-design-linked-list.ts
More file actions
116 lines (101 loc) · 2.82 KB
/
Copy path707-design-linked-list.ts
File metadata and controls
116 lines (101 loc) · 2.82 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
/**
* Solution explanation:
* with singly linked list, use function to return node before index as helper
*/
class LLNode {
val: number;
next: LLNode | null;
constructor(val: number) {
this.val = val;
this.next = null;
}
}
class MyLinkedList {
head: LLNode | null;
tail: LLNode | null;
listSize: number;
constructor() {
this.head = null;
this.tail = null;
this.listSize = 0;
}
private findNodeBeforeIndex(index: number): LLNode | null {
if (index >= this.listSize) return null;
const dummy = new LLNode(0);
dummy.next = this.head;
let current = dummy;
for (let i = 0; i < index; i++) {
current = current.next!;
}
return current;
}
get(index: number): number {
if (index < 0 || index >= this.listSize) return -1;
const before = this.findNodeBeforeIndex(index);
return before.next?.val ?? -1;
}
addAtHead(val: number): void {
const node = new LLNode(val);
node.next = this.head;
this.head = node;
if (this.head.next === null) {
this.tail = this.head;
}
this.listSize++;
}
addAtTail(val: number): void {
const node = new LLNode(val);
if (this.tail) {
this.tail.next = node;
this.tail = this.tail.next;
} else {
this.head = node;
this.tail = node;
}
this.listSize++;
}
addAtIndex(index: number, val: number): void {
if (index > this.listSize) return;
if (index <= 0) {
this.addAtHead(val);
} else if (index == this.listSize) {
this.addAtTail(val);
} else {
const before = this.findNodeBeforeIndex(index);
const tmp = before.next;
const newNode = new LLNode(val);
before.next = newNode;
newNode.next = tmp;
if (index === 0) {
this.head = newNode;
}
this.listSize++;
}
}
deleteAtIndex(index: number): void {
if (index < 0 || index >= this.listSize) return;
const before = this.findNodeBeforeIndex(index)!;
if (this.listSize === 1) {
this.head = null;
this.tail = null;
} else {
before.next = before.next!.next;
if (index === 0) {
this.head = before.next;
}
if (index === this.listSize - 1) {
this.tail = before;
}
}
this.listSize--;
}
}
/**
* Your MyLinkedList object will be instantiated and called as such:
* var obj = new MyLinkedList()
* var param_1 = obj.get(index)
* obj.addAtHead(val)
* obj.addAtTail(val)
* obj.addAtIndex(index,val)
* obj.deleteAtIndex(index)
*/