{
  "type": "code_options",
  "author": "csdn.net",
  "source": "solution.md",
  "exercise_id": "fcc35be3fee74a98956c3c13721ce600",
  "keywords": "链表,双指针",
  "title": "分隔链表",
  "desc": [
    {
      "content": "\n<p>给你一个链表的头节点 <code>head</code> 和一个特定值<em> </em><code>x</code> ，请你对链表进行分隔，使得所有 <strong>小于</strong> <code>x</code> 的节点都出现在 <strong>大于或等于</strong> <code>x</code> 的节点之前。</p><p>你应当 <strong>保留</strong> 两个分区中每个节点的初始相对位置。</p><p> </p><p><strong>示例 1：</strong></p><img alt=\"\" src=\"https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/0000-0099/0086.Partition%20List/images/partition.jpg\" style=\"width: 662px; height: 222px;\" /><pre><strong>输入：</strong>head = [1,4,3,2,5,2], x = 3<strong><br />输出</strong>：[1,2,2,4,3,5]</pre><p><strong>示例 2：</strong></p><pre><strong>输入：</strong>head = [2,1], x = 2<strong><br />输出</strong>：[1,2]</pre><p> </p><p><strong>提示：</strong></p><ul>\t<li>链表中节点的数目在范围 <code>[0, 200]</code> 内</li>\t<li><code>-100 <= Node.val <= 100</code></li>\t<li><code>-200 <= x <= 200</code></li></ul>",
      "language": "markdown"
    }
  ],
  "answer": [
    {
      "content": "",
      "language": "java"
    }
  ],
  "prepared": [
    [
      {
        "content": "",
        "language": "java"
      }
    ],
    [
      {
        "content": "",
        "language": "java"
      }
    ],
    [
      {
        "content": "",
        "language": "java"
      }
    ]
  ],
  "template": {
    "content": "public class ListNode {\n\tint val;\n\tListNode next;\n\tListNode(int x) {\n\t\tval = x;\n\t}\n}\nclass Solution {\n\tpublic ListNode partition(ListNode head, int x) {\n\t\tListNode dummyHead1 = new ListNode(0);\n\t\tListNode dummyHead2 = new ListNode(0);\n\t\tListNode node1 = dummyHead1;\n\t\tListNode node2 = dummyHead2;\n\t\twhile (head != null) {\n\t\t\tif (head.val < x) {\n\t\t\t\tnode1.next = head;\n\t\t\t\thead = head.next;\n\t\t\t\tnode1 = node1.next;\n\t\t\t\tnode1.next = null;\n\t\t\t} else {\n\t\t\t\tnode2.next = head;\n\t\t\t\thead = head.next;\n\t\t\t\tnode2 = node2.next;\n\t\t\t\tnode2.next = null;\n\t\t\t}\n\t\t}\n\t\tnode1.next = dummyHead2.next;\n\t\treturn dummyHead1.next;\n\t}\n}",
    "language": "java"
  },
  "node_id": "dailycode-63f1dfb5737e4d0eb3afba4a75fecd8e",
  "license": "csdn.net",
  "created_at": 1637894158,
  "topic_link": "https://bbs.csdn.net/topics/600470229"
}