---
# System prepended metadata

title: 82-Remove Duplicates from Sorted List II
tags: [Medium]

---

# 82-Remove Duplicates from Sorted List II
###### tags: `Medium`
## Question
https://leetcode.com/problems/remove-duplicates-from-sorted-list-ii/
## Key
1. 因為要刪掉全部的重複數字，所以可能會刪掉目前節點，所以透過在一開始加入一個dummy node，讓我們能確保接下來兩個節點不重複，再將節點保留並移動到下一個節點做檢查，直到最後兩個節點

2. 最後return會以cur或dump是因為linked list才能從原來的第一個點開始
## Reference

## Solution
```cpp=
class Solution {
public:
    ListNode* deleteDuplicates(ListNode* head) {
        if(head == NULL || head->next == NULL) return head;
        // generate a dummy node and put it at first node
        ListNode *dump = new ListNode(-1);
        dump->next = head;
        head = dump;
//start examining duplicates
        int temp;
        while(head->next != NULL && head->next->next != NULL){
             if(head->next->val == head->next->next->val){
                 temp = head->next->val;
                 while(head->next != NULL && head->next->val == temp){
                     head->next = head->next->next;
                 }
             }else{
                 head = head->next;
             }
        }

        return dump->next;
    }
};
```