# 82. Remove Duplicates from Sorted List II

**<font color=red>难度: Medium</font>**

## 刷题内容

> 原题连接

* https://leetcode.com/problems/remove-duplicates-from-sorted-list-ii/description/

> 内容描述

```
Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list.

Example 1:

Input: 1->2->3->3->4->4->5
Output: 1->2->5
Example 2:

Input: 1->1->1->2->3
Output: 2->3
```

## 解题方案

> 思路 1
******- 时间复杂度: O(N)******- 空间复杂度: O(N)******


先用83题的思想得到所有unique的list，期间遇到重复的存在字典里面一下

然后遍历上面的unique的list，遇到在字典里面的node就跳过


beats 72.95%

```python
class Solution(object):
    def deleteDuplicates(self, head):
        """
        :type head: ListNode
        :rtype: ListNode
        """
        head_cp = head
        duplicate = {}
        while head:
            while head.next and head.next.val == head.val:
                duplicate[head.val] = 1
                head.next = head.next.next    # skip duplicated node
            head = head.next     # not duplicate of current node, move to next node
            
        dummy = prev = ListNode(None)
        prev.next = head
        while head_cp:
            if head_cp.val not in duplicate:
                prev.next = head_cp
                prev = prev.next
            head_cp = head_cp.next
        prev.next = None # 一定要记得最后要加上这一步，因为最后的head_cp可能是一个重复数字
        return dummy.next 
```

> 思路 2
******- 时间复杂度: O(N)******- 空间复杂度: O(1)******

更省时间的方法是用一个prev 和 cur 指针，这样loop一次即可解决问题。

beats 100%

```python
class Solution(object):
    def deleteDuplicates(self, head):
        """
        :type head: ListNode
        :rtype: ListNode
        """
        dummy = cur = prev = ListNode(None)
        while head:
            while head and ((head.val == prev.val) or (head.next and head.val == head.next.val)):
                prev = head
                head = head.next
            cur.next = head
            cur = cur.next
            prev = head
        return dummy.next
```

但是这里的第二个prev = head有点难以理解

我个人觉得更喜欢下面这个版本

```python
class Solution(object):
    def deleteDuplicates(self, head):
        """
        :type head: ListNode
        :rtype: ListNode
        """
        dummy = prev = cur = ListNode(None)
        while head:
            while head and ((head.val == prev.val) or (head.next and head.next.val == head.val)):
                prev = head
                head = head.next
            cur.next = head
            cur = cur.next
            if head:
                head = head.next  
            
        return dummy.next
```




> 思路 3
******- 时间复杂度: O(N)******- 空间复杂度: O(1)******

递归, beats 100%

```python
class Solution(object):
    def deleteDuplicates(self, head):
        """
        :type head: ListNode
        :rtype: ListNode
        """
        if not head:
            return None
        nxt, is_head_dup = head.next, False
        while nxt and nxt.val == head.val:
            nxt, is_head_dup = nxt.next, True
        head.next = self.deleteDuplicates(nxt)
        return head.next if is_head_dup else head
```
