{
  "type": "code_options",
  "author": "https://github.com/qiyuangong/leetcode",
  "source": "solution.md",
  "exercise_id": "9e3e461bc83f4444ab61d8a39acbf7ec",
  "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": "python"
    }
  ],
  "prepared": [
    [
      {
        "content": "",
        "language": "python"
      }
    ],
    [
      {
        "content": "",
        "language": "python"
      }
    ],
    [
      {
        "content": "",
        "language": "python"
      }
    ]
  ],
  "template": {
    "content": "class ListNode(object):\n\tdef __init__(self, x):\n\t\tself.val = x\n\t\tself.next = None\nclass LinkList:\n\tdef __init__(self):\n\t\tself.head=None\n\tdef initList(self, data):\n\t\tself.head = ListNode(data[0])\n\t\tr=self.head\n\t\tp = self.head\n\t\tfor i in data[1:]:\n\t\t\tnode = ListNode(i)\n\t\t\tp.next = node\n\t\t\tp = p.next\n\t\treturn r\n\tdef\tconvert_list(self,head):\n\t\tret = []\n\t\tif head == None:\n\t\t\treturn\n\t\tnode = head\n\t\twhile node != None:\n\t\t\tret.append(node.val)\n\t\t\tnode = node.next\n\t\treturn ret\nclass Solution(object):\n\tdef reverseKGroup(self, head, k):\n\t\tif head is None:\n\t\t\treturn None\n\t\tindex = 0\n\t\tlead, last = 0, 0\n\t\tpos = head\n\t\ttemp = ListNode(-1)\n\t\ttemp.next = head\n\t\thead = temp\n\t\tstart = head\n\t\twhile pos is not None:\n\t\t\tif index % k == k - 1:\n\t\t\t\tlast = pos.next\n\t\t\t\tstart = self.reverseList(start, last)\n\t\t\t\tpos = start\n\t\t\tpos = pos.next\n\t\t\tindex += 1\n\t\treturn head.next\n\tdef reverseList(self, head, end):\n\t\tpos = head.next\n\t\tlast = end\n\t\tnext_start = pos\n\t\twhile pos != end:\n\t\t\thead.next = pos\n\t\t\tlast_pos = pos\n\t\t\tpos = pos.next\n\t\t\tlast_pos.next = last\n\t\t\tlast = last_pos\n\t\treturn next_start\n# %%\nl = LinkList()\nhead = [1,2,3,4, 5]\nl1 = l.initList(head)\ns = Solution()\nprint(l.convert_list(s.reverseKGroup(l1, k = 2)))",
    "language": "python"
  },
  "node_id": "dailycode-6b7661f648ca47c7a6daec82ae8020f1",
  "license": "csdn.net",
  "created_at": 1637894160,
  "topic_link": "https://bbs.csdn.net/topics/600471012"
}