{ "question_id": 92, "question_title": "反转链表 II", "difficulty": "中等", "question_content": "给你单链表的头指针 head 和两个整数 leftright ,其中 left <= right 。请你反转从位置 left 到位置 right 的链表节点,返回 反转后的链表

 

示例 1:

\"\"
输入:head = [1,2,3,4,5], left = 2, right = 4
输出:
[1,4,3,2,5]

示例 2:

输入:head = [5], left = 1, right = 1
输出:
[5]

 

提示:

 

进阶: 你可以使用一趟扫描完成反转吗?

", "topic_link": "https://bbs.csdn.net/topics/600469826", "cpp": "#include \n#include \nstruct ListNode\n{\n\tint val;\n\tstruct ListNode *next;\n};\nstatic struct ListNode *reverseBetween(struct ListNode *head, int m, int n)\n{\n\tint i;\n\tstruct ListNode dummy;\n\tstruct ListNode *prev = &dummy;\n\tprev->next = head;\n\tfor (i = 1; i < m; i++)\n\t{\n\t\tprev = prev->next;\n\t}\n\tstruct ListNode *p = prev->next;\n\tfor (i = m; i < n; i++)\n\t{\n\t\tstruct ListNode *q = p->next;\n\t\tp->next = q->next;\n\t\tq->next = prev->next;\n\t\tprev->next = q;\n\t}\n\treturn dummy.next;\n}\nint main(int argc, char **argv)\n{\n\tif (argc < 3)\n\t{\n\t\tfprintf(stderr, \"Usage: ./test m n 1 2 3...\\n\");\n\t\texit(-1);\n\t}\n\tint i, count = argc - 3;\n\tstruct ListNode dummy;\n\tstruct ListNode *prev = &dummy;\n\tstruct ListNode *p;\n\tfor (i = 0; i < count; i++)\n\t{\n\t\tp = malloc(sizeof(*p));\n\t\tp->val = atoi(argv[i + 3]);\n\t\tp->next = NULL;\n\t\tprev->next = p;\n\t\tprev = p;\n\t}\n\tint m = atoi(argv[1]);\n\tint n = atoi(argv[2]);\n\tstruct ListNode *head = reverseBetween(dummy.next, m, n);\n\tfor (p = head; p != NULL; p = p->next)\n\t{\n\t\tprintf(\"%d \", p->val);\n\t}\n\tprintf(\"\\n\");\n\treturn 0;\n}", "java": "class Solution {\n\tpublic ListNode reverseBetween(ListNode head, int m, int n) {\n\t\tListNode dummy = new ListNode(0);\n\t\tdummy.next = head;\n\t\tListNode pre = dummy;\n\t\tfor (int i = 1; i < m; i++) {\n\t\t\tpre = pre.next;\n\t\t}\n\t\thead = pre.next;\n\t\tfor (int i = m; i < n; i++) {\n\t\t\tListNode nex = head.next;\n\t\t\thead.next = nex.next;\n\t\t\tnex.next = pre.next;\n\t\t\tpre.next = nex;\n\t\t}\n\t\treturn dummy.next;\n\t}\n}", "js": "", "python": "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)))", "status": 1, "keywords": "链表", "license": { "cpp": "https://github.com/begeekmyfriend/leetcode", "python": "https://github.com/qiyuangong/leetcode", "java": "https://blog.csdn.net/weixin_44833195/article/details/106370716" }, "notebook": { "cpp": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/91/91_cpp.ipynb?type=file", "python": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/91/91_python.ipynb?type=file", "java": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/91/91_java.ipynb?type=file" }, "notebook_enable": 1 }