C++ code using recursion (36ms)


  • 0
    X
    class Solution 
    {
    public:
        ListNode* removeElements(ListNode* head, int val) 
        {
            if(head == NULL)
                return head;
            else if(head->next == NULL)
            {
                if(head->val == val)
                {
                    delete head;
                    return NULL;
                }
                 return head;
            }
            ListNode* p = removeElements(head->next, val);
            if(head->val == val)
            {
                delete head;
                return p;
            }
            else
            {
                head->next = p;
                return head;
            }
        }
    };

Log in to reply
 

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