time O(n) space O(1) with explanation


  • 0
    X
    public ListNode deleteDuplicates(ListNode head) {
        // base case
        if (null == head || null == head.next)
            return head;
        /*
        Found duplicate node
        1. connect head.next.next to current node
        2. re-check the current node for case like [1,1,1,...]
         */
        if (head.val == head.next.val) {
            head.next = head.next.next;
            head = deleteDuplicates(head);
        }
        // check next node
        head.next = deleteDuplicates(head.next);
        return head;
    }
    

Log in to reply
 

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