-
Notifications
You must be signed in to change notification settings - Fork 0
/
020-swap_linklink_in_k_group.js
47 lines (44 loc) · 1.17 KB
/
020-swap_linklink_in_k_group.js
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
const ListNode = require('./helper.js').ListNode;
function swapK(head) {
let curNode = head;
let nextNode;
let preNode;
while (curNode) {
nextNode = curNode.next;
curNode.next = preNode;
preNode = curNode;
curNode = nextNode;
}
return head; // new tail
}
function swapKGroup(head, k) {
if (!head) return head;
const dummy = new ListNode();
dummy.next = head;
let lastEnd = dummy;
let swapStart = head;
while (true) {
let i = 0;
let p = swapStart;
while (++i < k && p.next) {
p = p.next;
}
let nextStart = p.next;
p.next = null; // point end of current chunk to null, and swap it
lastEnd.next = p;
if (i < k) {
lastEnd.next = swapStart;
nextStart = null;
} else {
const tail = swapK(swapStart);
tail.next = nextStart;
lastEnd = tail;
swapStart = nextStart;
}
if (!nextStart) return dummy.next;
}
}
const input = [1, 2, 3];
const list = ListNode.ArrayToList(input);
const result = swapKGroup(list, 3);
console.log(ListNode.ListToArray(result));