/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
public class Solution {
public ListNode deleteDuplicates(ListNode head) {
if (head == null) {
return null;
}
ListNode dummy = new ListNode(-1);
dummy.next = head;
ListNode ptr = dummy;
while (ptr.next != null && ptr.next.next != null) {
if (ptr.next.val == ptr.next.next.val) {
int val = ptr.next.val;
ptr.next = ptr.next.next.next;
while (ptr.next != null && ptr.next.val == val) {
ptr.next = ptr.next.next;
}
} else {
ptr = ptr.next;
}
}
return dummy.next;
}
}