Skip to content

Commit a2752ed

Browse files
committed
🐞 fix: 合并冲突
1 parent 9c88040 commit a2752ed

File tree

1 file changed

+32
-64
lines changed

1 file changed

+32
-64
lines changed

src/25.reverse-nodes-in-k-group/index.js

+32-64
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
const { showLogs } = require("../../common/utils/withTimeLog");
2-
const ListNode = require("../../common/structures/ListNode");
1+
const { showLogs } = require('../../common/utils/withTimeLog');
2+
const ListNode = require('../../common/structures/ListNode');
33
/**
44
* 25.K 个一组翻转链表
55
* 给你链表的头节点 head ,每 k 个节点一组进行翻转,请你返回修改后的链表。
@@ -33,72 +33,40 @@ const ListNode = require("../../common/structures/ListNode");
3333
* @param {number} k
3434
* @return {ListNode}
3535
*/
36-
<<<<<<< HEAD
3736
const myReverse = (head, tail) => {
38-
let prev = tail.next;
39-
let p = head;
40-
while (prev !== tail) {
41-
const nex = p.next;
42-
p.next = prev;
43-
prev = p;
44-
p = nex;
45-
}
46-
return [tail, head];
47-
}
48-
var reverseKGroup = function(head, k) {
49-
const hair = new ListNode(0);
50-
hair.next = head;
51-
let pre = hair;
52-
53-
while (head) {
54-
let tail = pre;
55-
// 查看剩余部分长度是否大于等于 k
56-
for (let i = 0; i < k; ++i) {
57-
tail = tail.next;
58-
if (!tail) {
59-
return hair.next;
60-
}
61-
}
62-
const nex = tail.next;
63-
[head, tail] = myReverse(head, tail);
64-
// 把子链表重新接回原链表
65-
pre.next = head;
66-
tail.next = nex;
67-
pre = tail;
68-
head = tail.next;
69-
}
70-
return hair.next;
71-
=======
72-
/**
73-
* @param {ListNode} head
74-
* @param {number} k
75-
* @return {ListNode}
76-
*/
37+
let prev = tail.next;
38+
let p = head;
39+
while (prev !== tail) {
40+
const nex = p.next;
41+
p.next = prev;
42+
prev = p;
43+
p = nex;
44+
}
45+
return [tail, head];
46+
};
7747
const reverseKGroup = function (head, k) {
78-
const stack = [];
79-
const dummy = { next: head };
80-
let pre = dummy;
81-
while (true) {
82-
let count = 0;
83-
let tmp = head;
84-
while (tmp && count < k) {
85-
stack.push(tmp);
86-
tmp = tmp.next;
87-
count++;
88-
}
89-
if (count != k) {
90-
pre.next = head;
91-
break;
92-
}
93-
while (stack.length > 0) {
94-
pre.next = stack.pop();
95-
pre = pre.next;
48+
const hair = new ListNode(0);
49+
hair.next = head;
50+
let pre = hair;
51+
52+
while (head) {
53+
let tail = pre;
54+
// 查看剩余部分长度是否大于等于 k
55+
for (let i = 0; i < k; ++i) {
56+
tail = tail.next;
57+
if (!tail) {
58+
return hair.next;
59+
}
9660
}
97-
pre.next = tmp;
98-
head = tmp;
61+
const nex = tail.next;
62+
[head, tail] = myReverse(head, tail);
63+
// 把子链表重新接回原链表
64+
pre.next = head;
65+
tail.next = nex;
66+
pre = tail;
67+
head = tail.next;
9968
}
100-
return dummy.next;
101-
>>>>>>> 1eb6e3c5ba35b22d7d3ef6893b6ace2d80050d6e
69+
return hair.next;
10270
};
10371

10472
/**

0 commit comments

Comments
 (0)