# [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;
}
};
```
Ddddddd_158
- 粉丝: 3163
- 资源: 729
最新资源
- MATLAB代码:计及电转气协同的含碳捕集与垃圾焚烧电厂优化调度 关键词:碳捕集 电厂 需求响应 优化调度 电转气协同调度 参考文档:《计及电转气协同的含碳捕集与垃圾焚烧电厂优化调度》完全复现
- 关键词:微网 优化调度 深度强化学习 A3C 需求响应 编程语言:python平台 主题:基于改进A3C算法的微网优化调度与需求响应管理 内容简介: 代码主要做的是基于深度强化学习的微网
- cruise软件模型,混动仿真模型,IMMD架构混联混动仿真模型,Cruise混动仿真模型,混联混动汽车动力性经济性仿真 关于模型 1.本模型是基于IMMD架构搭载的混联混动仿真模型,关于IMMD架
- C#上位机开发源码 上位机项目源代码 采用基于RS485通讯总线的ModbusRtu协议,支持用户权限管理、sqlite数据库、实时曲线、历史曲线、历史报表、导出Excel、主界面布局可调带记忆等功能
- 基于plc的污水处理,组态王动画仿真,带PLC源代码,组态王源代码,图纸,IO地址分配
- Rhino(犀牛)插件ladybug-tools-1-8-0
- 三相10Kw光伏并网逆变器 包含全套理图 PCB 源代码
- MATLAB代码:考虑P2G和碳捕集设备的热电联供综合能源系统优化调度模型 关键词:碳捕集 综合能源系统 电转气P2G 热电联产 低碳调度 参考文档:Modeling and Optimiza
- 永磁同步直线电机仿真实例,仿真教学 maxwell16.0版本 12槽11极 包括图中模型以及一个仿真设置要点word文档教程
- 基于mpx+vue+node.js的双端网盘系统的设计与实现源代码全套技术资料.zip
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈