> For the complete documentation index, see [llms.txt](https://nataliekung.gitbook.io/ladder_code/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://nataliekung.gitbook.io/ladder_code/l6lian-biao/remove-duplicates-from-sorted-list-ii.md).

# 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.

分析

比较cur和cur.next，删除当前节点cur，不动prev，否则移动prev。注意有个continue,处理连续不同dup的情况。

```
/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
class Solution {
    public ListNode deleteDuplicates(ListNode head) {
        ListNode dummy = new ListNode(0);
        dummy.next = head;
        boolean hasDup = false;
        ListNode prev = dummy, cur = head;
        while(cur != null){
            hasDup = false;
            while(cur != null && cur.next != null && cur.val == cur.next.val){
                hasDup = true;
                cur = cur.next;
                prev.next = cur;  
            }

            if(hasDup){
                cur = cur.next;
                prev.next = cur;  
                continue;//缺了这一行！！！
            }

            if(cur != null){
                prev = prev.next;
                cur = cur.next;              
            }

        }

        return dummy.next;
    }
}
```
