# 82. Remove Duplicates from Sorted List II

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

# Solution

Approach 1: Two pointers.

# Code (Python)

Approach 1:

# Code (C++)

Approach 1:

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode* deleteDuplicates(ListNode* head) {
        if (head == NULL)
            return head;
        ListNode dummyHead(0);
        dummyHead.next = head;
        ListNode *slow = &dummyHead;
        ListNode *fast = head;
        while (true)
        {
            if (!fast || fast->val != slow->next->val)
            {
                if (slow->next->next == fast)
                    slow = slow->next;
                else
                    slow->next = fast;
            }
            if (!fast)
                break;
            fast = fast->next;
        }
        return dummyHead.next;
    }
};