{
  "type": "code_options",
  "author": "https://github.com/qiyuangong/leetcode",
  "source": "solution.md",
  "exercise_id": "f6e4d27cc81543159b689e2dc81085c9",
  "keywords": "链表",
  "title": "反转链表 II",
  "desc": [
    {
      "content": "\n给你单链表的头指针 <code>head</code> 和两个整数 <code>left</code> 和 <code>right</code> ，其中 <code>left <= right</code> 。请你反转从位置 <code>left</code> 到位置 <code>right</code> 的链表节点，返回 <strong>反转后的链表</strong> 。<p> </p><p><strong>示例 1：</strong></p><img alt=\"\" src=\"https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/0000-0099/0092.Reverse%20Linked%20List%20II/images/rev2ex2.jpg\" style=\"width: 542px; height: 222px;\" /><pre><strong>输入：</strong>head = [1,2,3,4,5], left = 2, right = 4<strong><br />输出：</strong>[1,4,3,2,5]</pre><p><strong>示例 2：</strong></p><pre><strong>输入：</strong>head = [5], left = 1, right = 1<strong><br />输出：</strong>[5]</pre><p> </p><p><strong>提示：</strong></p><ul>\t<li>链表中节点数目为 <code>n</code></li>\t<li><code>1 <= n <= 500</code></li>\t<li><code>-500 <= Node.val <= 500</code></li>\t<li><code>1 <= left <= right <= n</code></li></ul><p> </p><p><strong>进阶：</strong> 你可以使用一趟扫描完成反转吗？</p>",
      "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 reverseBetween(self, head, m, n):\n\t\t\"\"\"\n\t\t:type head: ListNode\n\t\t:type m: int\n\t\t:type n: int\n\t\t:rtype: ListNode\n\t\t\"\"\"\n\t\tif m == n:\n\t\t\treturn head\n\t\tsplit_node, prev, curr = None, None, head\n\t\tcount = 1\n\t\twhile count <= m and curr is not None:\n\t\t\tif count == m:\n\t\t\t\tsplit_node = prev\n\t\t\tprev = curr\n\t\t\tcurr = curr.next\n\t\t\tcount += 1\n\t\ttail, next_node = prev, None\n\t\twhile curr is not None and count <= n:\n\t\t\tnext_temp = curr.next\n\t\t\tcurr.next = prev\n\t\t\tprev = curr\n\t\t\tcurr = next_temp\n\t\t\tcount += 1\n\t\tif split_node is not None:\n\t\t\tsplit_node.next = prev\n\t\tif tail is not None:\n\t\t\ttail.next = curr\n\t\tif m == 1:\n\t\t\treturn prev\n\t\treturn head\n# %%\nl = LinkList()\nlist1 = [1,2,3,4,5]\nl1 = l.initList(list1)\nleft = 2\nright = 4\ns = Solution()\nprint(l.convert_list(s.reverseBetween(l1, left, right)))",
    "language": "python"
  },
  "node_id": "dailycode-5e08f34544f041569745d22d11919871",
  "license": "csdn.net",
  "created_at": 1637894159,
  "topic_link": "https://bbs.csdn.net/topics/600469826"
}