public static ListNode deleteDuplicates(ListNode head) {//passed after consulting the answer
        ListNode prev = new ListNode (0x7fffffff);
        prev.next = head;
        head = prev;//save the head
        ListNode n1 = head;
        while (n1.next != null) {
            ListNode n2 = n1.next;
            while (n2.next != null && n2.next.val == n2.val) {
                n2 = n2.next;
            }
            if (n1.next != n2) n1.next = n2.next;
            else n1 = n1.next;//go on to the next page
        }
        return head.next;
    }