2022-05-02 23:42:43 +08:00
|
|
|
|
<p>给你链表的头节点 <code>head</code> ,每 <code>k</code><em> </em>个节点一组进行翻转,请你返回修改后的链表。</p>
|
2022-03-27 20:56:26 +08:00
|
|
|
|
|
2022-05-02 23:42:43 +08:00
|
|
|
|
<p><code>k</code> 是一个正整数,它的值小于或等于链表的长度。如果节点总数不是 <code>k</code><em> </em>的整数倍,那么请将最后剩余的节点保持原有顺序。</p>
|
2022-03-27 20:56:26 +08:00
|
|
|
|
|
2022-05-02 23:42:43 +08:00
|
|
|
|
<p>你不能只是单纯的改变节点内部的值,而是需要实际进行节点交换。</p>
|
2022-03-27 20:56:26 +08:00
|
|
|
|
|
2022-05-02 23:42:43 +08:00
|
|
|
|
<p> </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>
|
|
|
|
|
|
2022-05-02 23:42:43 +08:00
|
|
|
|
<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>
|
2022-05-02 23:42:43 +08:00
|
|
|
|
<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>
|
|
|
|
|
|
2022-05-02 23:42:43 +08:00
|
|
|
|
<p> </p>
|
|
|
|
|
<strong>提示:</strong>
|
2022-03-27 20:56:26 +08:00
|
|
|
|
|
|
|
|
|
<ul>
|
2022-05-02 23:42:43 +08:00
|
|
|
|
<li>链表中的节点数目为 <code>n</code></li>
|
|
|
|
|
<li><code>1 <= k <= n <= 5000</code></li>
|
|
|
|
|
<li><code>0 <= Node.val <= 1000</code></li>
|
2022-03-27 20:56:26 +08:00
|
|
|
|
</ul>
|
|
|
|
|
|
2022-05-02 23:42:43 +08:00
|
|
|
|
<p> </p>
|
|
|
|
|
|
|
|
|
|
<p><strong>进阶:</strong>你可以设计一个只用 <code>O(1)</code> 额外内存空间的算法解决此问题吗?</p>
|
2022-03-27 20:56:26 +08:00
|
|
|
|
|
|
|
|
|
<ul>
|
|
|
|
|
</ul>
|