Share my solution using one loop:

Look at this example:

{1,1,1,2,2,3,4}

Make a dummy node before 1.

{0,1,1,1,2,2,3,4}

pre:0

cur:1

Move cur until value of cur != cur.next

Now cur stops at the first 2

Using repeat to indicate if jumping from a repeated number.

Here repeat is true, so we only let pre.next point to 2

Keep moving cur, it will stops at 3.

Now the repeat is still true, so we let pre.next point to 3

This also means that 1,1,1,2,2 has been by passed. since pre.next is also dummy.next

we keep moving, now cur is at 4

Now finally the repeat is false, we can move pre to pre.next(from dummy move to 3)

loop end, because we don't touch 3.next, it will still be 4

So the answer is 3,4

public ListNode deleteDuplicates(ListNode head) { if(head == null) return head; ListNode dummy = new ListNode(0); dummy.next = head; ListNode pre = dummy, cur = head; boolean repeat = false; while(cur.next != null){ if(cur.val == cur.next.val){ repeat = true; cur = cur.next; } else { cur = cur.next; if(!repeat){ pre = pre.next; } pre.next = cur; repeat = false; } } if(repeat) pre.next = null; return dummy.next; }