Clean C++ Recursive Solution


  • 0
    N
     ListNode* deleteDuplicates(ListNode* head) {
            
            if( head ){
                
                if( head->next && head->next->val == head->val){
                    ListNode* tmp = head->next;       
                    head->next = tmp->next;  // skip the same node
                    delete tmp;              // free memory 
                    deleteDuplicates( head ); 
                 }        
                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.