Simple 12ms recursive Cpp solution (beats 71%)


  • 1
    R
    class Solution {
    public:
        ListNode* deleteDuplicates(ListNode* head) {
            if ((head == NULL) || (head->next == NULL))
                return head;
            if (head->val == head->next->val)
            {
                    head->next = head->next->next;
                    head = deleteDuplicates(head);
            }       
            else 
                    head->next = deleteDuplicates(head->next);
       }
            
    };

  • 0
    R

    That's neat, but this just unlinks the duplicates; doesn't delete them


Log in to reply
 

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