Recursive solution without while-loop


  • 0
    Z
    class Solution {
    public:
        bool cur_duplicated = false;
        bool next_duplicated = false;
        ListNode* deleteDuplicates(ListNode* head) {
            if(!head){
                return NULL;
            }
            if(head->next && head->val == head->next->val){
                cur_duplicated = true;
                next_duplicated = true;
            }else if(next_duplicated){
                cur_duplicated = true;
                next_duplicated = false;
            }else{
                cur_duplicated = false;
                next_duplicated = false;
            }
            if(cur_duplicated){
                return deleteDuplicates(head->next);
            }else{
                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.