1
0
mirror of https://gitee.com/coder-xiaomo/leetcode-problemset synced 2025-01-11 02:58:13 +08:00
Code Issues Projects Releases Wiki Activity GitHub Gitee
leetcode-problemset/leetcode-cn/problem (Chinese)/K 个一组翻转链表 [reverse-nodes-in-k-group].html

40 lines
1.3 KiB
HTML
Raw Normal View History

<p>给你链表的头节点 <code>head</code> ,每&nbsp;<code>k</code><em>&nbsp;</em>个节点一组进行翻转,请你返回修改后的链表。</p>
2022-03-27 20:56:26 +08:00
<p><code>k</code> 是一个正整数,它的值小于或等于链表的长度。如果节点总数不是&nbsp;<code>k</code><em>&nbsp;</em>的整数倍,那么请将最后剩余的节点保持原有顺序。</p>
2022-03-27 20:56:26 +08:00
<p>你不能只是单纯的改变节点内部的值,而是需要实际进行节点交换。</p>
2022-03-27 20:56:26 +08:00
<p>&nbsp;</p>
2022-03-27 20:56:26 +08:00
<p><strong>示例 1</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/10/03/reverse_ex1.jpg" style="width: 542px; height: 222px;" />
<pre>
<strong>输入:</strong>head = [1,2,3,4,5], k = 2
<strong>输出:</strong>[2,1,4,3,5]
</pre>
<p><strong>示例 2</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2020/10/03/reverse_ex2.jpg" style="width: 542px; height: 222px;" /></p>
2022-03-27 20:56:26 +08:00
<pre>
<strong>输入:</strong>head = [1,2,3,4,5], k = 3
<strong>输出:</strong>[3,2,1,4,5]
2022-03-27 20:56:26 +08:00
</pre>
<p>&nbsp;</p>
<strong>提示:</strong>
2022-03-27 20:56:26 +08:00
<ul>
<li>链表中的节点数目为 <code>n</code></li>
<li><code>1 &lt;= k &lt;= n &lt;= 5000</code></li>
<li><code>0 &lt;= Node.val &lt;= 1000</code></li>
2022-03-27 20:56:26 +08:00
</ul>
<p>&nbsp;</p>
<p><strong>进阶:</strong>你可以设计一个只用 <code>O(1)</code> 额外内存空间的算法解决此问题吗?</p>
2022-03-27 20:56:26 +08:00
<ul>
</ul>