-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlinkedlist.ts
132 lines (121 loc) · 3.07 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
class SingleLinkedList {
private counts = 0;
private head: SingleNode | null = null;
constructor() {}
get(index: number): number {
const invalidIndex = index + 1 > this.counts;
const noHead = this.isNull(this.head);
if (invalidIndex || noHead) {
return -1;
}
let result = -1;
let loopNode = this.head as SingleNode;
let tempIndex = 0;
while (tempIndex <= index) {
if (tempIndex === index) {
result = loopNode.val;
tempIndex = index + 1;
} else if (loopNode.next) {
loopNode = loopNode.next;
}
tempIndex += 1;
}
return result;
}
addAtHead(val: number): void {
if (this.isNull(this.head)) {
const newHead = new SingleNode(val, null);
this.head = newHead;
this.counts += 1;
return;
}
const originalHead = this.head;
const newHead = new SingleNode(val, originalHead);
this.head = newHead;
this.counts += 1;
}
addAtTail(val: number): void {
if (this.isNull(this.head)) {
this.addAtHead(val);
return;
}
const newNode = new SingleNode(val, null);
let loopNode = this.head as SingleNode;
while (loopNode.next) {
loopNode = loopNode.next;
}
loopNode.next = newNode;
this.counts += 1;
}
addAtIndex(index: number, val: number): void {
if (index === 0) {
this.addAtHead(val);
return;
}
if (index > this.counts) {
return;
}
let preNode = this.head as SingleNode;
let preNodeIndex = 0;
while (preNodeIndex < index) {
if (preNodeIndex + 1 === index) {
const preNodeNext = preNode.next;
const newNode = new SingleNode(val, preNodeNext);
preNode.next = newNode;
this.counts += 1;
preNodeIndex = index;
} else if (preNode.next) {
preNode = preNode.next;
}
preNodeIndex += 1;
}
}
deleteAtIndex(index: number): void {
if (!this.head || index >= this.counts) {
return;
}
if (index === 0) {
const newHead = this.head.next;
this.head = newHead;
this.counts -= 1;
return;
}
let preNode = this.head as SingleNode;
let preNodeIndex = 0;
while (preNodeIndex < index) {
if (preNodeIndex + 1 === index) {
const deletedNode = preNode.next;
const newNextNode = deletedNode ? deletedNode.next : null;
if (deletedNode) {
deletedNode.next = null;
}
preNode.next = newNextNode;
this.counts -= 1;
preNodeIndex = index;
} else if (preNode.next) {
preNode = preNode.next;
}
preNodeIndex += 1;
}
}
private isNull(val): boolean {
return val === null;
}
}
class SingleNode {
constructor(val: number, next: SingleNode | null) {
this.val = val;
this.next = next;
}
val: number;
next: SingleNode | null;
}
/**
* SingleLinkedList object will be instantiated and called as such:
* var obj = new SingleLinkedList()
* var param_1 = obj.get(index)
* obj.addAtHead(val)
* obj.addAtTail(val)
* obj.addAtIndex(index,val)
* obj.deleteAtIndex(index)
*/