solution.json 3.0 KB
Newer Older
每日一练社区's avatar
每日一练社区 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
{
  "type": "code_options",
  "author": "https://github.com/begeekmyfriend/leetcode",
  "source": "solution.md",
  "exercise_id": "04e3a587f60c4cc5aeb21d77711430f7",
  "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": "cpp"
    }
  ],
  "prepared": [
    [
      {
        "content": "",
        "language": "cpp"
      }
    ],
    [
      {
        "content": "",
        "language": "cpp"
      }
    ],
    [
      {
        "content": "",
        "language": "cpp"
      }
    ]
  ],
  "template": {
    "content": "#include <stdio.h>\n#include <stdlib.h>\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}",
    "language": "cpp"
  },
  "node_id": "dailycode-eef49c9c9f0943f2a27bb9f4dfb22718",
  "license": "csdn.net",
  "created_at": 1637894158,
  "topic_link": "https://bbs.csdn.net/topics/600469826"
}