Skip to content

Commit fe9e415

Browse files
committed
feat: add ts solution to lc problem: No.0025,0143
- No.0025.Reverse Nodes in k-Group - No.0143.Reorder List
1 parent ae17a6a commit fe9e415

File tree

5 files changed

+298
-0
lines changed

5 files changed

+298
-0
lines changed

solution/0000-0099/0025.Reverse Nodes in k-Group/README.md

+49
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,55 @@ function reverse(head: ListNode, tail: ListNode) {
208208
}
209209
```
210210

211+
```ts
212+
/**
213+
* Definition for singly-linked list.
214+
* class ListNode {
215+
* val: number
216+
* next: ListNode | null
217+
* constructor(val?: number, next?: ListNode | null) {
218+
* this.val = (val===undefined ? 0 : val)
219+
* this.next = (next===undefined ? null : next)
220+
* }
221+
* }
222+
*/
223+
224+
function reverseKGroup(head: ListNode | null, k: number): ListNode | null {
225+
if (k === 1) {
226+
return head;
227+
}
228+
229+
const dummy = new ListNode(0, head);
230+
let root = dummy;
231+
while (root != null) {
232+
let pre = root;
233+
let cur = root;
234+
235+
let count = 0;
236+
while (count !== k) {
237+
count++;
238+
cur = cur.next;
239+
if (cur == null) {
240+
return dummy.next;
241+
}
242+
}
243+
244+
const nextRoot = pre.next;
245+
pre.next = cur;
246+
247+
let node = nextRoot;
248+
let next = node.next;
249+
node.next = cur.next;
250+
while (node != cur) {
251+
[next.next, node, next] = [node, next, next.next];
252+
}
253+
root = nextRoot;
254+
}
255+
256+
return dummy.next;
257+
}
258+
```
259+
211260
### **Go**
212261

213262
```go

solution/0000-0099/0025.Reverse Nodes in k-Group/README_EN.md

+49
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,55 @@ function reverse(head: ListNode, tail: ListNode) {
178178
}
179179
```
180180

181+
```ts
182+
/**
183+
* Definition for singly-linked list.
184+
* class ListNode {
185+
* val: number
186+
* next: ListNode | null
187+
* constructor(val?: number, next?: ListNode | null) {
188+
* this.val = (val===undefined ? 0 : val)
189+
* this.next = (next===undefined ? null : next)
190+
* }
191+
* }
192+
*/
193+
194+
function reverseKGroup(head: ListNode | null, k: number): ListNode | null {
195+
if (k === 1) {
196+
return head;
197+
}
198+
199+
const dummy = new ListNode(0, head);
200+
let root = dummy;
201+
while (root != null) {
202+
let pre = root;
203+
let cur = root;
204+
205+
let count = 0;
206+
while (count !== k) {
207+
count++;
208+
cur = cur.next;
209+
if (cur == null) {
210+
return dummy.next;
211+
}
212+
}
213+
214+
const nextRoot = pre.next;
215+
pre.next = cur;
216+
217+
let node = nextRoot;
218+
let next = node.next;
219+
node.next = cur.next;
220+
while (node != cur) {
221+
[next.next, node, next] = [node, next, next.next];
222+
}
223+
root = nextRoot;
224+
}
225+
226+
return dummy.next;
227+
}
228+
```
229+
181230
### **Go**
182231

183232
```go

solution/0100-0199/0143.Reorder List/README.md

+80
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,86 @@ var reorderList = function (head) {
286286
};
287287
```
288288

289+
### **TypeScript**
290+
291+
```ts
292+
/**
293+
* Definition for singly-linked list.
294+
* class ListNode {
295+
* val: number
296+
* next: ListNode | null
297+
* constructor(val?: number, next?: ListNode | null) {
298+
* this.val = (val===undefined ? 0 : val)
299+
* this.next = (next===undefined ? null : next)
300+
* }
301+
* }
302+
*/
303+
304+
/**
305+
Do not return anything, modify head in-place instead.
306+
*/
307+
function reorderList(head: ListNode | null): void {
308+
const arr = [];
309+
let node = head;
310+
while (node.next != null) {
311+
arr.push(node);
312+
node = node.next;
313+
}
314+
let l = 0;
315+
let r = arr.length - 1;
316+
while (l < r) {
317+
const start = arr[l];
318+
const end = arr[r];
319+
[end.next.next, start.next, end.next] = [start.next, end.next, null];
320+
l++;
321+
r--;
322+
}
323+
}
324+
```
325+
326+
```ts
327+
/**
328+
* Definition for singly-linked list.
329+
* class ListNode {
330+
* val: number
331+
* next: ListNode | null
332+
* constructor(val?: number, next?: ListNode | null) {
333+
* this.val = (val===undefined ? 0 : val)
334+
* this.next = (next===undefined ? null : next)
335+
* }
336+
* }
337+
*/
338+
339+
/**
340+
Do not return anything, modify head in-place instead.
341+
*/
342+
function reorderList(head: ListNode | null): void {
343+
let slow = head;
344+
let fast = head;
345+
// 找到中心节点
346+
while (fast != null && fast.next != null) {
347+
slow = slow.next;
348+
fast = fast.next.next;
349+
}
350+
// 反转节点
351+
let next = slow.next;
352+
slow.next = null;
353+
while (next != null) {
354+
[next.next, slow, next] = [slow, next, next.next];
355+
}
356+
// 合并
357+
let left = head;
358+
let right = slow;
359+
while (right.next != null) {
360+
const next = left.next;
361+
left.next = right;
362+
right = right.next;
363+
left.next.next = next;
364+
left = left.next.next;
365+
}
366+
}
367+
```
368+
289369
### **...**
290370

291371
```

solution/0100-0199/0143.Reorder List/README_EN.md

+80
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,86 @@ var reorderList = function (head) {
269269
};
270270
```
271271

272+
### **TypeScript**
273+
274+
```ts
275+
/**
276+
* Definition for singly-linked list.
277+
* class ListNode {
278+
* val: number
279+
* next: ListNode | null
280+
* constructor(val?: number, next?: ListNode | null) {
281+
* this.val = (val===undefined ? 0 : val)
282+
* this.next = (next===undefined ? null : next)
283+
* }
284+
* }
285+
*/
286+
287+
/**
288+
Do not return anything, modify head in-place instead.
289+
*/
290+
function reorderList(head: ListNode | null): void {
291+
const arr = [];
292+
let node = head;
293+
while (node.next != null) {
294+
arr.push(node);
295+
node = node.next;
296+
}
297+
let l = 0;
298+
let r = arr.length - 1;
299+
while (l < r) {
300+
const start = arr[l];
301+
const end = arr[r];
302+
[end.next.next, start.next, end.next] = [start.next, end.next, null];
303+
l++;
304+
r--;
305+
}
306+
}
307+
```
308+
309+
```ts
310+
/**
311+
* Definition for singly-linked list.
312+
* class ListNode {
313+
* val: number
314+
* next: ListNode | null
315+
* constructor(val?: number, next?: ListNode | null) {
316+
* this.val = (val===undefined ? 0 : val)
317+
* this.next = (next===undefined ? null : next)
318+
* }
319+
* }
320+
*/
321+
322+
/**
323+
Do not return anything, modify head in-place instead.
324+
*/
325+
function reorderList(head: ListNode | null): void {
326+
let slow = head;
327+
let fast = head;
328+
329+
while (fast != null && fast.next != null) {
330+
slow = slow.next;
331+
fast = fast.next.next;
332+
}
333+
334+
let next = slow.next;
335+
slow.next = null;
336+
while (next != null) {
337+
[next.next, slow, next] = [slow, next, next.next];
338+
}
339+
340+
let left = head;
341+
let right = slow;
342+
while (right.next != null) {
343+
const next = left.next;
344+
left.next = right;
345+
right = right.next;
346+
left.next.next = next;
347+
left = left.next.next;
348+
}
349+
}
350+
```
351+
272352
### **...**
273353

274354
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* class ListNode {
4+
* val: number
5+
* next: ListNode | null
6+
* constructor(val?: number, next?: ListNode | null) {
7+
* this.val = (val===undefined ? 0 : val)
8+
* this.next = (next===undefined ? null : next)
9+
* }
10+
* }
11+
*/
12+
13+
/**
14+
Do not return anything, modify head in-place instead.
15+
*/
16+
function reorderList(head: ListNode | null): void {
17+
let slow = head;
18+
let fast = head;
19+
20+
while (fast != null && fast.next != null) {
21+
slow = slow.next;
22+
fast = fast.next.next;
23+
}
24+
25+
let next = slow.next;
26+
slow.next = null;
27+
while (next != null) {
28+
[next.next, slow, next] = [slow, next, next.next];
29+
}
30+
31+
let left = head;
32+
let right = slow;
33+
while (right.next != null) {
34+
const next = left.next;
35+
left.next = right;
36+
right = right.next;
37+
left.next.next = next;
38+
left = left.next.next;
39+
}
40+
}

0 commit comments

Comments
 (0)