Simple C++ Solution


  • 1
    C

    class Solution {
    public:
    ListNode* removeElements(ListNode* head, int val) {

        if(head == NULL)return NULL;
        ListNode* runner = head;
        ListNode* prev = NULL;
        
        ListNode* root = head;
        
        while(runner)
        {
            if(runner->val == val)
            {
                if(prev == NULL)
                {
                    runner =  runner->next;
                    root = runner;
                }
                else
                {
                    prev->next = runner->next;
                    runner =  runner->next;
                }
            }
            else
            {
               prev = runner;
               runner = runner->next;
            }
            
        }
        return root;
        
    }
    

    };


Log in to reply
 

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