C++ AC code, 43ms


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

Log in to reply
 

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