My clean c++ solution, may help you


  • 0
    ListNode* removeElements(ListNode* head, int val) {
        while(head && head->val == val)
        {
            ListNode * d1 = head;
            head = head->next;
            delete d1;
        }
        if(!head) return head;
        ListNode * p = head;
        while(p->next)
        {
            if(p->next->val == val)
            {
                ListNode * d2 = p->next;
                p->next = p->next->next;
                delete d2;
            }
            else
            {
                p = p->next;
            }
        }
        return head;
    }

Log in to reply
 

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