public ListNode deleteDuplicates(ListNode head) {
        // Start typing your Java solution below
        // DO NOT write main() function
        if (head == null || head.next == null) return head;

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

        return dummy.next;
    }