Created
March 24, 2017 14:23
-
-
Save linnykoleh/6b5a19b2b4bbd0f7c0f8ab7f7f01cf2c to your computer and use it in GitHub Desktop.
Remove duplicates from sorted list II
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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