-
-
Notifications
You must be signed in to change notification settings - Fork 8.8k
/
Copy pathSolution.ts
48 lines (46 loc) · 1.18 KB
/
Solution.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
/**
* Definition for singly-linked list.
* class ListNode {
* val: number
* next: ListNode | null
* constructor(val?: number, next?: ListNode | null) {
* this.val = (val===undefined ? 0 : val)
* this.next = (next===undefined ? null : next)
* }
* }
*/
function reverseKGroup(head: ListNode | null, k: number): ListNode | null {
let dummy = new ListNode(0, head);
let pre = dummy;
// pre->head-> ... ->tail-> next
while (head != null) {
let tail = pre;
for (let i = 0; i < k; ++i) {
tail = tail.next;
if (tail == null) {
return dummy.next;
}
}
let t = tail.next;
[head, tail] = reverse(head, tail);
// set next
pre.next = head;
tail.next = t;
// set new pre and new head
pre = tail;
head = t;
}
return dummy.next;
}
function reverse(head: ListNode, tail: ListNode) {
let cur = head;
let pre = tail.next;
// head -> next -> ... -> tail -> pre
while (pre != tail) {
let t = cur.next;
cur.next = pre;
pre = cur;
cur = t;
}
return [tail, head];
}