---
# System prepended metadata

title: 0082. Remove Duplicates from Sorted List II
tags: [Leetcode, Linked List, Medium]

---

# 0082. Remove Duplicates from Sorted List II
###### tags: `Leetcode` `Medium` `Linked List`
Link: https://leetcode.com/problems/remove-duplicates-from-sorted-list-ii/
## 思路
在linked list的前面加一个没有意义的node来防止edge case（head node要被删掉）
用prev来存重复的node构成的sublist的前一个node
![](https://i.imgur.com/V14MGs7.png)

## Code
```java=
class Solution {
    public ListNode deleteDuplicates(ListNode head) {
        ListNode dummyNode = new ListNode(0);
        dummyNode.next = head;
        ListNode prev = dummyNode, curr = head;
        while(curr!=null){
            while(curr.next!=null && curr.val==curr.next.val){
                curr = curr.next;
            }
            if(prev.next==curr){
                prev = prev.next;
            }
            else{
                prev.next = curr.next;
            }
            curr = curr.next;
        }
        return dummyNode.next;
    }
}
```