Skip to content

Commit 767ff00

Browse files
committed
一刷25
1 parent 652a61e commit 767ff00

File tree

7 files changed

+84
-20
lines changed

7 files changed

+84
-20
lines changed

README.adoc

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -201,12 +201,12 @@ TIP: **公众号的微信号是: `jikerizhi`**。__因为众所周知的原因
201201
|Medium
202202
|
203203

204-
//|{counter:codes}
205-
//|{leetcode_base_url}/reverse-nodes-in-k-group/[25. Reverse Nodes in k-Group^]
206-
//|{source_base_url}/_0025_ReverseNodesInKGroup.java[Java]
207-
//|{doc_base_url}/0025-reverse-nodes-in-k-group.adoc[题解]
208-
//|Hard
209-
//|
204+
|{counter:codes}
205+
|{leetcode_base_url}/reverse-nodes-in-k-group/[25. Reverse Nodes in k-Group^]
206+
|{source_base_url}/_0025_ReverseNodesInKGroup.java[Java]
207+
|{doc_base_url}/0025-reverse-nodes-in-k-group.adoc[题解]
208+
|Hard
209+
|
210210

211211
|{counter:codes}
212212
|{leetcode_base_url}/remove-duplicates-from-sorted-array/[26. Remove Duplicates from Sorted Array^]

docs/0025-reverse-nodes-in-k-group.adoc

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,43 @@
11
[#0025-reverse-nodes-in-k-group]
2-
= 25. Reverse Nodes in k-Group
2+
= 25. K 个一组翻转链表
33

4-
{leetcode}/problems/reverse-nodes-in-k-group/[LeetCode - Reverse Nodes in k-Group^]
4+
https://leetcode.cn/problems/reverse-nodes-in-k-group/[LeetCode - 25. K 个一组翻转链表 ^]
55

6-
Given a linked list, reverse the nodes of a linked list _k_ at a time and return its modified list.
6+
给你链表的头节点 `head`,每 `k` 个节点一组进行翻转,请你返回修改后的链表。
77

8-
_k_ is a positive integer and is less than or equal to the length of the linked list. If the number of nodes is not a multiple of _k_ then left-out nodes in the end should remain as it is.
8+
`k` 是一个正整数,它的值小于或等于链表的长度。如果节点总数不是 `k` 的整数倍,那么请将最后剩余的节点保持原有顺序。
99

10+
你不能只是单纯的改变节点内部的值,而是需要实际进行节点交换。
1011

12+
*示例 1:*
1113

14+
image::images/0025-01.jpg[{image_attr}]
1215

13-
*Example:*
16+
....
17+
输入:head = [1,2,3,4,5], k = 2
18+
输出:[2,1,4,3,5]
19+
....
1420

15-
Given this linked list: `1->2->3->4->5`
21+
*示例 2:*
1622

17-
For _k_ = 2, you should return: `2->1->4->3->5`
23+
image::images/0025-02.jpg[{image_attr}]
1824

19-
For _k_ = 3, you should return: `3->2->1->4->5`
25+
....
26+
输入:head = [1,2,3,4,5], k = 3
27+
输出:[3,2,1,4,5]
28+
....
2029

21-
*Note:*
30+
*提示:*
2231

32+
* 链表中的节点数目为 `n`
33+
* `+1 <= k <= n <= 5000+`
34+
* `+0 <= Node.val <= 1000+`
2335
24-
* Only constant extra memory is allowed.
25-
* You may not alter the values in the list's nodes, only nodes itself may be changed.
36+
**进阶:**你可以设计一个只用 stem:[O(1)] 额外内存空间的算法解决此问题吗?
2637

38+
== 思路分析
39+
40+
先统计链表长度,然后根据长度将链表切段,每段使用递归反转链表,最后再接上剩余不够一段的部分。代码如下:
2741

2842
[[src-0025]]
2943
[tabs]
@@ -42,7 +56,7 @@ include::{sourcedir}/_0025_ReverseNodesInKGroup.java[tag=answer]
4256
// --
4357
// [{java_src_attr}]
4458
// ----
45-
// include::{sourcedir}/_0015_3Sum_20.java[tag=answer]
59+
// include::{sourcedir}/_0025_ReverseNodesInKGroup_2.java[tag=answer]
4660
// ----
4761
// --
4862
====

docs/images/0025-01.jpg

18.9 KB
Loading

docs/images/0025-02.jpg

18.4 KB
Loading

docs/index.adoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ include::0023-merge-k-sorted-lists.adoc[leveloffset=+1]
126126

127127
include::0024-swap-nodes-in-pairs.adoc[leveloffset=+1]
128128

129-
// include::0025-reverse-nodes-in-k-group.adoc[leveloffset=+1]
129+
include::0025-reverse-nodes-in-k-group.adoc[leveloffset=+1]
130130

131131
include::0026-remove-duplicates-from-sorted-array.adoc[leveloffset=+1]
132132

logbook/202503.adoc

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ endif::[]
6262

6363
|{counter:codes2503}
6464
|{leetcode_base_url}/remove-duplicate-letters/[316. 去除重复字母^]
65-
|{doc_base_url}/0316-remove-duplicate-letters.adoc[题解]
65+
|{doc_base_url}/316. 去除重复字母[题解]
6666
|❌ 完全想不到单调栈!
6767

6868
|{counter:codes2503}
@@ -120,6 +120,11 @@ endif::[]
120120
|{doc_base_url}/0215-kth-largest-element-in-an-array.adoc[题解]
121121
|✅ 快速选择
122122

123+
|{counter:codes2503}
124+
|{leetcode_base_url}/reverse-nodes-in-k-group/[25. K 个一组翻转链表^]
125+
|{doc_base_url}/0025-reverse-nodes-in-k-group.adoc[题解]
126+
|✅ 分段递归反转,在拼接
127+
123128
|===
124129

125130
截止目前,本轮练习一共完成 {codes2503} 道题。
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,52 @@
11
package com.diguage.algo.leetcode;
22

3+
import com.diguage.algo.util.ListNode;
4+
35
public class _0025_ReverseNodesInKGroup {
46
// tag::answer[]
7+
/**
8+
* @author D瓜哥 · https://www.diguage.com
9+
* @since 2025-04-02 16:12:21
10+
*/
11+
public ListNode reverseKGroup(ListNode head, int k) {
12+
int size = 0;
13+
ListNode cur = head;
14+
while (cur != null) {
15+
size++;
16+
cur = cur.next;
17+
}
18+
int sect = size / k;
19+
// 如果长度不够一段,则直接返回
20+
if (sect == 0) {
21+
return head;
22+
}
23+
// 将链表分段做反转,再拼接
24+
ListNode dummy = new ListNode(0);
25+
ListNode tail = dummy;
26+
cur = head;
27+
for (int i = 0; i < sect; i++) {
28+
ListNode[] result = reverse(cur, cur, null, k);
29+
tail.next = result[0];
30+
tail = result[1];
31+
cur = result[2];
32+
}
33+
tail.next = cur; // 把最后的尾部给接上
34+
return dummy.next;
35+
}
536

37+
// 参数:1、head,2、curr,3、prev,4、k
38+
// 结果:1、head,2、tail(反转过来,参数的 head 就是 tail),3、next
39+
private ListNode[] reverse(ListNode head,
40+
ListNode cur, ListNode prev, int k) {
41+
if (k == 0) {
42+
return new ListNode[]{null, head, cur};
43+
}
44+
ListNode[] result = reverse(head, cur.next, cur, k - 1);
45+
if (k == 1) {
46+
result[0] = cur;
47+
}
48+
cur.next = prev;
49+
return result;
50+
}
651
// end::answer[]
752
}

0 commit comments

Comments
 (0)