82. Remove Duplicates from Sorted List II
三个指针,一个pre,一个cur,一个next(下一个不同的数字)
如果不遇到重复数字,那么三个都往前移一格
如果遇到重复数字,那么pre.next = next,这个时候pre并没有改变
1 public ListNode deleteDuplicates(ListNode head) { 2 if(head == null) { 3 return null; 4 } 5 ListNode dummy = new ListNode(-1); 6 dummy.next = head; 7 boolean dup = false; 8 ListNode next = head; 9 ListNode cur = head; 10 ListNode pre = dummy; 11 while(cur != null) { 12 next = cur.next; 13 while(next != null && next.val == cur.val) { 14 next = next.next; 15 dup = true; 16 } 17 if(dup) { 18 pre.next = next; 19 } else { 20 pre = cur; 21 } 22 dup = false; 23 cur = next; 24 } 25 return dummy.next; 26 }