solution.md 4.1 KB
Newer Older
每日一练社区's avatar
每日一练社区 已提交
1
# 删除排序链表中的重复元素 II
F
fix bug  
feilong 已提交
2

每日一练社区's avatar
每日一练社区 已提交
3
<p>存在一个按升序排列的链表,给你这个链表的头节点 <code>head</code> ,请你删除链表中所有存在数字重复情况的节点,只保留原始链表中 <strong>没有重复出现</strong><em> </em>的数字。</p><p>返回同样按升序排列的结果链表。</p><p> </p><p><strong>示例 1:</strong></p><img alt="" src="https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/0000-0099/0082.Remove%20Duplicates%20from%20Sorted%20List%20II/images/linkedlist1.jpg" style="width: 500px; height: 142px;" /><pre><strong>输入:</strong>head = [1,2,3,3,4,4,5]<strong><br />输出:</strong>[1,2,5]</pre><p><strong>示例 2:</strong></p><img alt="" src="https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/0000-0099/0082.Remove%20Duplicates%20from%20Sorted%20List%20II/images/linkedlist2.jpg" style="width: 500px; height: 205px;" /><pre><strong>输入:</strong>head = [1,1,1,2,3]<strong><br />输出:</strong>[2,3]</pre><p> </p><p><strong>提示:</strong></p><ul>	<li>链表中节点数目在范围 <code>[0, 300]</code> 内</li>	<li><code>-100 <= Node.val <= 100</code></li>	<li>题目数据保证链表已经按升序排列</li></ul>
每日一练社区's avatar
每日一练社区 已提交
4
<p>以下<font color="red">错误</font>的选项是?</p>
F
fix bug  
feilong 已提交
5

每日一练社区's avatar
每日一练社区 已提交
6
## aop
F
fix bug  
feilong 已提交
7

每日一练社区's avatar
每日一练社区 已提交
8
### before
F
fix bug  
feilong 已提交
9

每日一练社区's avatar
每日一练社区 已提交
10 11 12 13 14 15 16 17 18 19 20 21
```cpp
#include <bits/stdc++.h>
using namespace std;

struct ListNode
{
    int val;
    ListNode *next;
    ListNode(int x) : val(x), next(NULL) {}
};
```
### after
F
fix bug  
feilong 已提交
22

每日一练社区's avatar
每日一练社区 已提交
23 24 25 26 27
```cpp

```

## 答案
F
fix bug  
feilong 已提交
28

每日一练社区's avatar
每日一练社区 已提交
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
```cpp
class Solution
{
public:
    ListNode *deleteDuplicates(ListNode *head)
    {
        if (!head || !head->next)
            return head;
        auto dummy = new ListNode(-1);
        dummy->next = head;
        auto pre = dummy, cur = head;
        while (cur && cur->next)
        {
            if (cur->val != cur->next->val)
            {
                pre = cur;
                cur = cur->next;
            }
            else
            {
                while (cur->next && cur->val == cur->next->val)
                {
                    cur = cur->next;
                }
            }
        }
        return dummy->next;
    }
};
```
## 选项

F
fix bug  
feilong 已提交
61

每日一练社区's avatar
每日一练社区 已提交
62
### A
F
fix bug  
feilong 已提交
63

每日一练社区's avatar
每日一练社区 已提交
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
```cpp
class Solution
{
public:
    ListNode *deleteDuplicates(ListNode *head)
    {
        if (!head || !head->next)
            return head;
        if (head->val == head->next->val)
        {
            while (head->next && head->val == head->next->val)
            {
                head = head->next;
            }
            return deleteDuplicates(head->next);
        }
        else
            head->next = deleteDuplicates(head->next);
        return head;
    }
};
```

### B
F
fix bug  
feilong 已提交
88

每日一练社区's avatar
每日一练社区 已提交
89 90 91 92 93 94 95 96 97 98 99 100 101 102 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
```cpp
class Solution
{
public:
    ListNode *deleteDuplicates(ListNode *head)
    {
        ListNode *new_head = new ListNode(0);
        ListNode *cur = new_head;
        ListNode *temp1 = head;
        while (temp1)
        {
            ListNode *temp2 = temp1->next;
            if (!temp2)
            {
                cur->next = temp1;
                temp1 = temp1->next;
                cur = cur->next;
            }
            else if (temp2 && temp1->val != temp2->val)
            {
                cur->next = temp1;
                temp1 = temp1->next;
                cur = cur->next;
            }
            else
            {
                while (temp2 && temp1->val == temp2->val)
                {
                    temp2 = temp2->next;
                }
                temp1 = temp2;
            }
        }
        cur->next = NULL;
        return new_head->next;
    }
};
```

### C
F
fix bug  
feilong 已提交
129

每日一练社区's avatar
每日一练社区 已提交
130 131 132 133 134 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
```cpp
class Solution
{
public:
    ListNode *deleteDuplicates(ListNode *head)
    {
        if (head == NULL || head->next == NULL)
            return head;
        ListNode *L = new ListNode(0);
        L->next = head;

        ListNode *slow = L;
        while (slow->next)
        {
            ListNode *fast = slow->next;
            while (fast->next && fast->val == fast->next->val)
            {
                fast = fast->next;
            }
            if (slow->next == fast)
                slow = slow->next;
            else
            {
                slow->next = fast->next;
            }
        }
        return L->next;
    }
};
```