-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
153 lines (149 loc) · 3.47 KB
/
index.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
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
const { ListNode, make_list_node } = require('../utils/');
/**
* 思路:
*
* 1.将分成 k 个一组装进一个数组中
* 2.从后往前遍历数组
* 3.再把组数组reverse
* 4.返回第一组的最后一项
*
* @param {ListNode} head
* @param {number} k
* @return {ListNode}
*/
var reverseKGroup1 = function (head, k) {
if (k === 1) {
return head;
}
const arr = [];
let chunk = [];
let node = head;
while (node) {
chunk.push(node);
if (chunk.length === k) {
arr.push(chunk);
chunk = [];
}
node = node.next;
}
if (chunk.length < k && chunk.length > 0) {
arr.push(chunk);
}
if (arr.length === 1 && arr[0].length < k) {
return head;
}
for (let i = arr.length - 2; i >= 0; i--) {
chunk = arr[i];
for (let j = chunk.length - 1; j > 0; j--) {
chunk[j].next = chunk[j - 1];
}
const arr_temp = arr[i + 1];
chunk[0].next = arr[i + 1][arr[i + 1].length - 1];
}
chunk = arr[arr.length - 1];
if (chunk.length === k) {
for (let j = chunk.length - 1; j > 0; j--) {
chunk[j].next = chunk[j - 1];
}
chunk[0].next = null;
} else {
if (arr[arr.length - 2]) arr[arr.length - 2][0].next = chunk[0];
}
return arr[0][arr[0].length - 1];
};
/**
* 思路:
*
* 参考了一些别人的答案,感觉思路更清晰简单一些,这里试着自己实现一遍
*
* 这里用了递归的思想,我们当前的最后最后一项需要知道下一个reverse的第一项,
*
* 每k个我们自己做一次reverse
*
* 1.现获取后面的第一项 temp_node
* 2.缓存head 的 下一项 next_node
* 3.head.next 指向 temp_node
* 4.temp_node = head
* 5.head = next_node
*
* @param {ListNode} head
* @param {number} k
* @return {ListNode}
*/
var reverseKGroup2 = function (head, k) {
if (k === 1) {
return head;
}
let tempNode = head;
let i = 1;
while (i < k && tempNode) {
tempNode = tempNode.next;
i++;
}
if (i === k && tempNode) {
tempNode = reverseKGroup(tempNode.next, k);
while (i > 0) {
const next= head.next;
head.next = tempNode;
tempNode = head;
head = next;
i--;
}
return tempNode;
}
return head;
}
/**
*
* @param {ListNode} head
* @param {number} k
* @return {ListNode}
*/
var reverseKGroup = function (head, k) {
if (k === 1) {
return head;
}
let tmpNode = head;
let length = 0;
while (tmpNode) {
length++;
tmpNode = tmpNode.next;
}
let groupCount = Math.floor(length / k);
if (groupCount === 0) {
return head;
}
let preLastNode = head;
let cur = head;
let j = 0;
while (j < groupCount) {
let i = 1;
let pre = null;
const firstNode = cur;
while (i < k) {
const next = cur.next;
cur.next = pre;
pre = cur;
cur = next;
i++;
}
j++;
if (j === 1) {
head = cur;
} else {
preLastNode.next = cur;
preLastNode = firstNode;
}
const next = cur.next;
cur.next = pre;
cur = next;
}
preLastNode.next = cur;
return head;
}
let test = make_list_node([1, 2, 3, 4,5]);
test = reverseKGroup(test, 5);
while (test) {
console.log(test.val);
test = test.next;
}