{
  "type": "code_options",
  "author": "csdn.net",
  "source": "solution.md",
  "exercise_id": "68cf7e0dee9e4009a8bcd13e99e501d2",
  "keywords": "递归,链表",
  "title": "K 个一组翻转链表",
  "desc": [
    {
      "content": "\n<p>给你一个链表，每 <em>k </em>个节点一组进行翻转，请你返回翻转后的链表。</p><p><em>k </em>是一个正整数，它的值小于或等于链表的长度。</p><p>如果节点总数不是 <em>k </em>的整数倍，那么请将最后剩余的节点保持原有顺序。</p><p><strong>进阶：</strong></p><ul>\t<li>你可以设计一个只使用常数额外空间的算法来解决此问题吗？</li>\t<li><strong>你不能只是单纯的改变节点内部的值</strong>，而是需要实际进行节点交换。</li></ul><p> </p><p><strong>示例 1：</strong></p><img alt=\"\" src=\"https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/0000-0099/0025.Reverse%20Nodes%20in%20k-Group/images/reverse_ex1.jpg\" style=\"width: 542px; height: 222px;\" /><pre><strong>输入：</strong>head = [1,2,3,4,5], k = 2<strong><br />输出：</strong>[2,1,4,3,5]</pre><p><strong>示例 2：</strong></p><img alt=\"\" src=\"https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/0000-0099/0025.Reverse%20Nodes%20in%20k-Group/images/reverse_ex2.jpg\" style=\"width: 542px; height: 222px;\" /><pre><strong>输入：</strong>head = [1,2,3,4,5], k = 3<strong><br />输出：</strong>[3,2,1,4,5]</pre><p><strong>示例 3：</strong></p><pre><strong>输入：</strong>head = [1,2,3,4,5], k = 1<strong><br />输出：</strong>[1,2,3,4,5]</pre><p><strong>示例 4：</strong></p><pre><strong>输入：</strong>head = [1], k = 1<strong><br />输出：</strong>[1]</pre><ul></ul><p><strong>提示：</strong></p><ul>\t<li>列表中节点的数量在范围 <code>sz</code> 内</li>\t<li><code>1 <= sz <= 5000</code></li>\t<li><code>0 <= Node.val <= 1000</code></li>\t<li><code>1 <= k <= sz</code></li></ul>",
      "language": "markdown"
    }
  ],
  "answer": [
    {
      "content": "",
      "language": "java"
    }
  ],
  "prepared": [
    [
      {
        "content": "",
        "language": "java"
      }
    ],
    [
      {
        "content": "",
        "language": "java"
      }
    ],
    [
      {
        "content": "",
        "language": "java"
      }
    ]
  ],
  "template": {
    "content": "\npublic class ListNode {\n\tint val;\n\tListNode next;\n\n\tListNode() {\n\t}\n\n\tListNode(int val) {\n\t\tthis.val = val;\n\t}\n\n\tListNode(int val, ListNode next) {\n\t\tthis.val = val;\n\t\tthis.next = next;\n\t}\n}\n\nclass Solution {\n\tpublic ListNode reverseKGroup(ListNode head, int k) {\n\t\tif (head == null) {\n\t\t\treturn null;\n\t\t}\n\t\tListNode a = head, b = head;\n\n\t\tfor (int i = 0; i < k; i++) {\n\t\t\tif (b == null) {\n\t\t\t\treturn a;\n\t\t\t}\n\t\t\tb = b.next;\n\t\t}\n\n\t\tListNode newHead = reverse(a, b);\n\n\t\ta.next = reverseKGroup(b, k);\n\t\treturn newHead;\n\t}\n\n\tpublic ListNode reverse(ListNode a, ListNode b) {\n\t\tListNode pre, cur, nxt;\n\t\tpre = null;\n\t\tcur = a;\n\t\tnxt = a;\n\t\twhile (nxt != b) {\n\t\t\tnxt = cur.next;\n\t\t\tcur.next = pre;\n\t\t\tpre = cur;\n\t\t\tcur = nxt;\n\t\t}\n\t\treturn pre;\n\t}\n}",
    "language": "java"
  },
  "node_id": "dailycode-e3a7c5b5f5724a47a7de4643ceb4b4d4",
  "license": "csdn.net",
  "created_at": 1637894159,
  "topic_link": "https://bbs.csdn.net/topics/600471012"
}