Leetcode 0025.Reverse Nodes in k-Group(python)
25. Reverse Nodes in k-Group
题目
Given the head of a linked list, reverse the nodes of the list k at a time, and return the modified list.
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.
You may not alter the values in the list's nodes, only nodes themselves may be changed.
Example 1:
1 | Input: head = [1,2,3,4,5], k = 2 |
Example 2:
1 | Input: head = [1,2,3,4,5], k = 3 |
题目大意
给定一个链表,每 k 个节点一组进行翻转,并返回翻转后的链表。如果节点总数不是 k 的倍数,那么最后剩余的节点保持原有顺序。要求不能修改节点的值,只能改变节点指针。
你选用何种方法解题?
本题的核心是分组翻转链表。
| 方法 | 时间复杂度 | 空间复杂度 | 是否推荐 |
|---|---|---|---|
| 迭代法(统计节点数) | O(n) | O(1) | 推荐 |
| 头插法 | O(n) | O(1) | 推荐 |
方法选择理由:
- 迭代法(统计节点数):逻辑清晰,代码易于理解
- 头插法:更高效,不需要提前统计节点数
解题过程
问题分析
输入:链表头节点 head,整数 k
输出:每 k 个节点一组翻转后的链表
关键约束:
- 不能修改节点的值
- 最后不足 k 个节点保持不变
核心洞察
- 迭代法:先统计节点总数,然后逐组翻转,每组翻转后正确连接
- 头插法:在遍历过程中直接进行翻转,不需要提前统计节点数
迭代法算法流程
以 head = [1,2,3,4,5], k = 2 为例:
1 | 统计节点数: n = 5 |
头插法算法流程
以 head = [1,2,3,4,5], k = 2 为例:
1 | 初始化: dummy -> 1 -> 2 -> 3 -> 4 -> 5 |
这些方法具体怎么运用?
方法一:迭代法(统计节点数)(推荐)
核心逻辑:
- 统计节点数:遍历链表统计总节点数
n - 初始化指针:
dummy = ListNode(next=head),p0 = dummy,pre = None,cur = head - 分组翻转:
- 当
n >= k时,翻转 k 个节点 - 使用头插法翻转:
cur.next = pre,pre = cur,cur = nxt
- 当
- 连接翻转后的组:
nxt = p0.next(翻转前的第一个节点,现在是最后一个)nxt.next = cur(连接下一组)p0.next = pre(连接翻转后的组)p0 = nxt(移动到下一组的前一个节点)
- 返回结果:返回
dummy.next
方法二:头插法
核心逻辑:
- 创建哑节点:
dummy = ListNode(next=head) - 初始化指针:
pre = dummy,cur = head - 遍历链表:
- 检查剩余节点是否 >= k
- 使用头插法将后面的 k-1 个节点依次插入到 pre 后面
- 返回结果:返回
dummy.next
边界情况处理:
| 边界情况 | 处理方式 |
|---|---|
| k = 1 | 不需要翻转,直接返回原链表 |
| 链表长度 < k | 不需要翻转,直接返回原链表 |
| 链表长度是 k 的倍数 | 全部翻转 |
复杂度
| 方法 | 时间复杂度 | 空间复杂度 | 说明 |
|---|---|---|---|
| 迭代法(统计节点数) | O(n) | O(1) | n 是链表长度 |
| 头插法 | O(n) | O(1) | 不需要提前统计 |
总结与最佳选择
最快算法:两种方法时间复杂度相同。
工程最优选择:迭代法(统计节点数)。理由如下:
- 逻辑清晰:代码结构清晰,易于理解和维护
- 可读性强:分组处理逻辑明确
各方法适用场景:
- 迭代法(统计节点数):代码清晰,适合大多数情况
- 头插法:更简洁,不需要提前统计节点数
Code
方法一:迭代法(统计节点数)(推荐)
1 | from typing import Optional |
方法二:头插法
1 | from typing import Optional |
All articles on this blog are licensed under CC BY-NC-SA 4.0 unless otherwise stated.

