Skip to content

Instantly share code, notes, and snippets.

@linnykoleh
Created March 24, 2017 14:23
Show Gist options
  • Save linnykoleh/6b5a19b2b4bbd0f7c0f8ab7f7f01cf2c to your computer and use it in GitHub Desktop.
Save linnykoleh/6b5a19b2b4bbd0f7c0f8ab7f7f01cf2c to your computer and use it in GitHub Desktop.
Remove duplicates from sorted list II
public static ListNode deleteDuplicates(ListNode head) {
if(head == null) return null;
ListNode fake = new ListNode(-1);
fake.next = head;
ListNode pre = fake;
ListNode cur = head;
while(cur != null){
while(cur.next != null && cur.val == cur.next.val){
cur = cur.next;
}
if(pre.next == cur){
pre = pre.next;
}else{
pre.next = cur.next;
}
cur = cur.next;
}
return fake.next;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment