my C++ iterative solution


  • 0
    K
    class Solution {
    public:
        ListNode* deleteDuplicates(ListNode* head) {
            ListNode* ans = helper(head);
            return ans;
        }
    private:
        ListNode *helper(ListNode* head){
            ListNode *ans;
            if(!head )
                return head;
            if(head->next == NULL)
                return head;
            if( head->val != head->next->val)
                ans = head;
            else{
                while(head->next and head->val == head -> next -> val){
                    head = head -> next;
                }
                ans = helper(head->next);
            }
            if(ans)
                ans->next = helper(ans->next);
            return ans;
        }
    };
    

Log in to reply
 

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