# [25. Reverse Nodes in k-Group (Hard)](https://leetcode.com/problems/reverse-nodes-in-k-group/)
<p>Given a linked list, reverse the nodes of a linked list <em>k</em> at a time and return its modified list.</p>
<p><em>k</em> 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 <em>k</em> then left-out nodes, in the end, should remain as it is.</p>
<p>You may not alter the values in the list's nodes, only nodes themselves may be changed.</p>
<p> </p>
<p><strong>Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/10/03/reverse_ex1.jpg" style="width: 542px; height: 222px;">
<pre><strong>Input:</strong> head = [1,2,3,4,5], k = 2
<strong>Output:</strong> [2,1,4,3,5]
</pre>
<p><strong>Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/10/03/reverse_ex2.jpg" style="width: 542px; height: 222px;">
<pre><strong>Input:</strong> head = [1,2,3,4,5], k = 3
<strong>Output:</strong> [3,2,1,4,5]
</pre>
<p><strong>Example 3:</strong></p>
<pre><strong>Input:</strong> head = [1,2,3,4,5], k = 1
<strong>Output:</strong> [1,2,3,4,5]
</pre>
<p><strong>Example 4:</strong></p>
<pre><strong>Input:</strong> head = [1], k = 1
<strong>Output:</strong> [1]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the list is in the range <code>sz</code>.</li>
<li><code>1 <= sz <= 5000</code></li>
<li><code>0 <= Node.val <= 1000</code></li>
<li><code>1 <= k <= sz</code></li>
</ul>
<p> </p>
<strong>Follow-up:</strong> Can you solve the problem in O(1) extra memory space?
**Companies**:
[Amazon](https://leetcode.com/company/amazon), [Microsoft](https://leetcode.com/company/microsoft), [Facebook](https://leetcode.com/company/facebook), [Apple](https://leetcode.com/company/apple), [eBay](https://leetcode.com/company/ebay), [Adobe](https://leetcode.com/company/adobe), [Google](https://leetcode.com/company/google), [Walmart Labs](https://leetcode.com/company/walmart-labs)
**Related Topics**:
[Linked List](https://leetcode.com/tag/linked-list/), [Recursion](https://leetcode.com/tag/recursion/)
**Similar Questions**:
* [Swap Nodes in Pairs (Medium)](https://leetcode.com/problems/swap-nodes-in-pairs/)
* [Swapping Nodes in a Linked List (Medium)](https://leetcode.com/problems/swapping-nodes-in-a-linked-list/)
## Solution 1.
```cpp
// OJ: https://leetcode.com/problems/reverse-nodes-in-k-group/
// Author: github.com/lzl124631x
// Time: O(N)
// Space: O(1)
class Solution {
public:
ListNode* reverseKGroup(ListNode* head, int k) {
ListNode h, *tail = &h;
while (head) {
auto prev = tail;
int i = 0;
for (auto p = head; i < k && p; ++i, p = p->next);
if (i < k) {
tail->next = head;
break;
}
for (int i = 0; i < k && head; ++i) {
auto node = head;
head = head->next;
node->next = prev->next;
prev->next = node;
}
while (tail->next) tail = tail->next;
}
return h.next;
}
};
```
## Solution 2.
```cpp
// OJ: https://leetcode.com/problems/reverse-nodes-in-k-group/
// Author: github.com/lzl124631x
// Time: O(N)
// Space: O(1)
class Solution {
private:
bool splitAtK(ListNode *head, int k, ListNode *&nextHead) {
while (--k && head) head = head->next;
if (!head) return false;
nextHead = head->next;
head->next = NULL;
return true;
}
pair<ListNode*, ListNode*> reverseList(ListNode *head) {
ListNode dummy(0), *tail = NULL;
while (head) {
ListNode *p = head;
head = head->next;
if (!tail) tail = p;
p->next = dummy.next;
dummy.next = p;
}
return { dummy.next, tail };
}
public:
ListNode* reverseKGroup(ListNode* head, int k) {
ListNode *nextHead, dummy(0), *tail = &dummy;
while (splitAtK(head, k, nextHead)) {
auto p = reverseList(head);
tail->next = p.first;
tail = p.second;
head = nextHead;
}
tail->next = head;
return dummy.next;
}
};
```
![avatar](https://profile-avatar.csdnimg.cn/default.jpg!1)
Ddddddd_158
- 粉丝: 3165
- 资源: 729
最新资源
- 【TCN回归预测】TCN时间卷积神经网络数据回归预测(多输入单输出)【含Matlab源码 2317期】.zip
- 【电力负荷预测】EEMD+IWOA+LSSVM电力负荷预测【含Matlab源码 1810期】.zip
- c语言入门,简要的写明c语言的入门
- 永磁同步直线电机PMLSM矢量控制滑模控制SVPWM仿真模型的研究 外环控制器:PI与滑膜控制策略的效果分析与三相电流波形优化,永磁同步直线电机PMLSM矢量控制滑模控制SVPWM仿真模型的研究 外环
- 2025易支付新版PHP网站源码.zip
- Magic Formula与Dugoff模型MF模型对比实验的Matlab建模与程序代码,含纯纵滑、纯侧偏及复合工况Simulink仿真,Magic Formula与Dugoff模型对比实验的Matl
- 这个是有关于ppocr4的使用推理模型
- comsol技术引领的双目标函数流热优化与液冷板结构设计的融合探讨,关注最小化平均温度与最小流体功率耗散的无量纲化案例及参考文献分享交流 ,双目标函数流热优化在液冷板结构设计中的应用-最小化平均温度
- 基于`typecho开发的导航源码-BeaconNav 导航主题
- 基于NSDBO算法的MATLAB多目标优化程序包-集成多种测试函数与评价指标的工程应用案例研究,NSDBO算法的Matlab实现:多目标测试函数与评价指标的全面研究及工程应用案例,非支配排序多目标蜣
- 房地产营改增税负率测算表
- 永磁同步直线电机PMLSM矢量控制滑模控制SVPWM仿真模型研究:外环控制器性能分析与三相电流波形优化(附参考文献),永磁同步直线电机PMLSM矢量控制滑模控制SVPWM仿真模型研究-外环控制器性能
- 电气安全知识问答-11003559.pdf
- sql语言的入门教程 欢迎下载
- 基于ECMS和EEMS控制策略的燃料电池能量管理仿真模型研究:多电动飞机应急电源系统分析,基于ECMS和EEMS策略的燃料电池能量管理系统的仿真与效果对比,基于ECMS控制策略的燃料电池能量管理 仿真
- 利用新算法PD近场动力学技术模拟三维复杂裂纹扩展过程:深入探索与精准预测,利用新算法PD模拟三维复杂裂纹扩展:近场动力学的创新应用与实践,用新算法pd 近场动力学模拟三维复杂裂纹扩展 ,核心关键词:新
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
![feedback](https://img-home.csdnimg.cn/images/20220527035711.png)
![feedback](https://img-home.csdnimg.cn/images/20220527035711.png)
![feedback-tip](https://img-home.csdnimg.cn/images/20220527035111.png)