-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSingleLinked.ts
200 lines (182 loc) · 5.51 KB
/
SingleLinked.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
import Node from '../00_common/Node';
export default class SingleLinked {
head: Node;
constructor() {
this.head = new Node('head');
}
findLastNode(): Node {
let currentValue: Node = this.head;
while (currentValue.next !== null) {
currentValue = currentValue.next;
}
return currentValue;
}
removeFirstNode() {
this.head.next = this.head.next.next;
}
getData() {
let currentNode = this.head?.next;
if (!currentNode) {
return null;
}
const tempArray = [];
while (currentNode.next !== null) {
tempArray.push(currentNode.data);
currentNode = currentNode.next;
}
tempArray.push(currentNode.data);
return tempArray.join(',');
}
/**
* append after the last node
* @param node
*/
append(node: Node) {
const lastNode = this.findLastNode();
lastNode.next = node;
}
findByValue(node: Node): Node {
let currentValue = this.head.next;
while (currentValue !== null && currentValue !== node) {
currentValue = currentValue.next;
}
return currentValue === null ? null : currentValue;
}
/**
* remove a given node
* @param node
*/
remove(node: Node) {
const previousNode = this.findPreviousValue(node);
if (previousNode) {
previousNode.next = previousNode.next.next;
} else {
throw new Error(`not found ${node}`);
}
}
private findPreviousValue(node: Node): Node {
let currentValue = this.head;
// check first node
if (this.head.next === node) {
return currentValue;
}
while (currentValue.next !== null && currentValue.next.next !== node) {
currentValue = currentValue.next;
}
return currentValue || null;
}
/**
* insert a new node before a given node
* @param newNode
* @param node
*/
insert(newNode: Node, node: Node) {
const previousValue = this.findPreviousValue(node);
if (previousValue) {
newNode.next = previousValue.next;
previousValue.next = newNode;
} else {
throw new Error('not found node');
}
}
reverse() {
// 哨兵节点
const root = new Node('head');
let currentValue = this.head.next;
while (currentValue !== null) {
const next = currentValue.next;
currentValue.next = root.next;
root.next = currentValue;
currentValue = next;
}
this.head = root;
}
// 参考(https://leetcode-cn.com/problems/linked-list-cycle-ii/solution/linked-list-cycle-ii-kuai-man-zhi-zhen-shuang-zhi-/)
checkCycleStart() {
let fast = this.head;
let slow = this.head;
while (fast !== null && fast.next !== null) {
slow = slow.next;
fast = fast.next.next;
if (slow === fast) {
break;
}
}
fast = this.head;
while (fast !== slow) {
fast = fast.next;
slow = slow.next;
}
return fast.data;
}
mergeSortedList(list1: SingleLinked, list2: SingleLinked) {
if (!list1) {
return list2;
}
if (!list2) {
return list1;
}
const root = new Node('head');
let currentList1 = list1.head.next;
let currentList2 = list2.head.next;
let currentRoot = root;
while (currentList1 !== null && currentList2 !== null) {
if (currentList1.data <= currentList2.data) {
currentRoot.next = currentList1;
currentList1 = currentList1.next;
} else if (currentList1.data > currentList2.data) {
currentRoot.next = currentList2;
currentList2 = currentList2.next;
}
currentRoot = currentRoot.next;
}
if (currentList1 == null) {
currentRoot.next = currentList2;
} else {
currentRoot.next = currentList1;
}
return root;
}
deleteByLastIndex(index: number) {
this.reverse();
let currentValue = this.head.next;
let pos = 1;
while (currentValue !== null && pos < index) {
currentValue = currentValue.next;
pos++;
}
const findValue = this.findByValue(currentValue);
const previousValue = this.findPreviousValue(currentValue);
previousValue.next = findValue.next;
this.reverse();
}
// 参考:https://leetcode-cn.com/problems/middle-of-the-linked-list/solution/lian-biao-de-zhong-jian-jie-dian-by-leetcode-solut/
findMiddleValue() {
let fast = this.head;
let slow = this.head;
while (fast.next !== null) {
fast = fast.next.next;
slow = slow.next;
}
return slow;
}
findPreviousNode(node: Node) {
let currentValue = this.head;
let nextValue = currentValue.next;
if (nextValue === node) {
return currentValue;
}
while (currentValue.next !== null && nextValue !== node) {
currentValue = currentValue.next;
nextValue = currentValue.next;
}
if (currentValue.next !== null) {
return currentValue;
} else {
return null;
}
}
clearLink() {
this.head.next = null;
}
}