public ListNode deleteDuplicates(ListNode head) {
        if (head == null || head.next == null)
            return head;
        ListNode ptr = new ListNode(-1);
        ListNode res = ptr;
        ptr.next = head;
        ListNode first = head, second = head.next;
        while (second != null) {
            boolean duplicate = false;
            while (second != null && first.val == second.val) {
                first = first.next;
                second = second.next;
                duplicate = true;
            }
            if (duplicate) {
                ptr.next = second;
                first = second;
                if (first != null) {
                    second = second.next;
                }
            } else {
                ptr = ptr.next;
                first = first.next;
                second = second.next;
            }
        }
        return res.next;
    }