1ms Java solution

  • 0

    Make tmp pointer points to head. Starting from tmp, drop all nodes after tmp that has the same value by adjusting tmp.next, then make tmp = tmp.next

    public ListNode deleteDuplicates(ListNode head) {
            if(head == null || head.next == null)
                return head;
            ListNode ptr1, ptr2;
            ptr1 = head;
            while(ptr1 != null) {
                ptr2 = ptr1.next;
                while(ptr2 != null && ptr1.val == ptr2.val)
                    ptr2 = ptr2.next;
                ptr1.next = ptr2;
                ptr1 = ptr1.next;
            return head;

Log in to reply

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.