C++ 12ms reclusive solution, short code


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

  • 0

    I don't think recursion is a good idea ^^


  • 0

    Well, I agree it is not a good idea. However, it is also not a bad idea for this problem. No big extra cost actually.


Log in to reply
 

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