solution.md 4.5 KB
Newer Older
1
# 分隔链表
2 3
<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>	<li>链表中节点的数目在范围 <code>[0, 200]</code> 内</li>	<li><code>-100 <= Node.val <= 100</code></li>	<li><code>-200 <= x <= 200</code></li></ul>
<p>以下错误的选项是?</p>
4 5 6
## aop
### before
```cpp
每日一练社区's avatar
每日一练社区 已提交
7 8
#include <bits/stdc++.h>
using namespace std;
9

每日一练社区's avatar
每日一练社区 已提交
10 11 12 13 14 15
struct ListNode
{
    int val;
    ListNode *next;
    ListNode(int x) : val(x), next(NULL) {}
};
16 17 18 19 20 21 22 23
```
### after
```cpp

```

## 答案
```cpp
每日一练社区's avatar
每日一练社区 已提交
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 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
class Solution
{
public:
    ListNode *partition(ListNode *head, int x)
    {
        if (!head || !head->next)
            return head;
        ListNode *p = head;
        ListNode *q = head;
        ListNode *qq = q;
        int flag = 0;
        while (q && q->val < x)
        {
            flag = 1;
            p = q;
            q = q->next;
        }
        while (q)
        {
            if (flag == 0 && q->val < x)
            {
                qq->next = q->next;
                q->next = p;
                p = q;
                head = p;
                q = qq->next;
                flag = 1;
            }
            else if (flag == 1 && q->val < x)
            {
                qq->next = q->next;
                q->next = p->next;
                p->next = q;
                p = p->next;
                q = qq->next;
            }
        }
        return head;
    }
};
64 65 66 67 68
```
## 选项

### A
```cpp
每日一练社区's avatar
每日一练社区 已提交
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
class Solution
{
public:
    ListNode *partition(ListNode *head, int x)
    {
        ListNode *dummy = new ListNode(-1);
        dummy->next = head;
        ListNode *pre = dummy, *cur;
        while (pre->next && pre->next->val < x)
            pre = pre->next;
        cur = pre;
        while (cur->next)
        {
            if (cur->next->val < x)
            {
                ListNode *tmp = cur->next;
                cur->next = tmp->next;
                tmp->next = pre->next;
                pre->next = tmp;
                pre = pre->next;
            }
            else
            {
                cur = cur->next;
            }
        }
        return dummy->next;
    }
};
98 99 100 101 102

```

### B
```cpp
每日一练社区's avatar
每日一练社区 已提交
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
class Solution
{
public:
    ListNode *partition(ListNode *head, int x)
    {
        ListNode *lessNode = new ListNode(-1);
        ListNode *moreNode = new ListNode(-1);
        ListNode *l = lessNode;
        ListNode *m = moreNode;
        while (head)
        {
            if (head->val >= x)
            {
                m->next = head;
                m = m->next;
            }
            else
            {
                l->next = head;
                l = l->next;
            }
            head = head->next;
        }
        l->next = moreNode->next;
        m->next = NULL;
        return lessNode->next;
    }
};
131 132 133 134
```

### C
```cpp
每日一练社区's avatar
每日一练社区 已提交
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
class Solution
{
public:
    ListNode *partition(ListNode *head, int x)
    {
        if (head == NULL || head->next == NULL)
            return head;
        ListNode *ahead, *p, *after, *p1;
        p = head;
        while (p && p->val < x)
        {
            ahead = p;
            p = p->next;
        }
        if (p == head)
            ahead = p;
        if (p)
            after = p->next;
        p1 = p;
        while (after)
        {
            if (after->val < x)
            {
                if (p == head)
                {
                    head = after;
                    ahead = head;
                }
                else
                {
                    ahead->next = after;
                    ahead = after;
                }
                p1->next = after->next;
            }
            else
            {
                p1 = after;
            }
            after = after->next;
        }
        if (ahead != p)
            ahead->next = p;
        return head;
    }
};
181
```