32 ms c++ simple solution


  • 1
    P
    class Solution {
    public:
        ListNode* removeElements(ListNode* head, int val) {
            
            ListNode * tmp = head;
            ListNode * prevTmp = NULL;
            
            while(tmp)
            {
                if(tmp->val == val)
                {
                    if(prevTmp)
                    {
                        prevTmp->next = tmp->next;
                        tmp = tmp->next;
                    }
                    else
                    {
                        head = tmp->next;
                        tmp = tmp->next;
                    }
                }
                else
                {
                    prevTmp = tmp;
                    tmp = tmp->next;
                }
            }
            
            return head;
        }
    };

Log in to reply
 

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