Skip to content

Commit c96d472

Browse files
committed
feat: 2074. Reverse Nodes in Even Length Groups : 链表
1 parent 066cf1d commit c96d472

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* function ListNode(val, next) {
4+
* this.val = (val===undefined ? 0 : val)
5+
* this.next = (next===undefined ? null : next)
6+
* }
7+
*/
8+
/**
9+
* @param {ListNode} head
10+
* @return {ListNode}
11+
*/
12+
const reverseEvenLengthGroups = function (head) {
13+
let a = head; let sz = 0
14+
while (a) sz++, a = a.next
15+
console.log(sz)
16+
let x = null; let y = head
17+
for (let i = 1; sz > 0; sz -= i, i++) {
18+
const len = Math.min(i, sz)
19+
let a = y; let b = a.next
20+
// console.log('case1', i, len, sz, x?.val, y?.val, a?.val, b?.val)
21+
for (let j = 0; j < len - 1; j++) {
22+
if (len % 2 === 0) {
23+
const c = b.next
24+
b.next = a
25+
a = b, b = c
26+
} else {
27+
a = a.next
28+
}
29+
}
30+
if (len % 2 === 0) {
31+
// console.log('case2', i, sz, len, x?.val, y.val, a.val, b?.val)
32+
x.next = a, y.next = b
33+
x = y, y = x.next
34+
} else {
35+
x = a, y = x.next
36+
}
37+
}
38+
39+
return head
40+
}

leetcode/残酷刷题/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,7 @@
172172
- 206. Reverse Linked List
173173
- 92. Reverse Linked List II
174174
- 25. Reverse Nodes in k-Group
175+
- 2074. Reverse Nodes in Even Length Groups
175176

176177
## Links
177178

0 commit comments

Comments
 (0)