Beats 99.09% easy understand C++ solution


  • 0
    N
    class Solution {
    public:
        ListNode* removeElements(ListNode* head, int val) {
            ListNode *p = new ListNode(-1);
            p->next = head;
            ListNode *cur = p->next;
            ListNode *pre = p;
            while(cur)
            {
                if(cur->val == val)
                {
                    pre->next = cur->next;
                    delete cur;
                    cur = pre->next;
                }
                else
                {
                    pre = cur;
                    cur = cur->next;
                }
            }
            return p->next;
        }
    };
    

Log in to reply
 

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