C++ 25ms


  • 0
    B
    class Solution {
    public:
        ListNode* removeElements(ListNode* head, int val) {
            if(!head) return head;
            while(head->val == val){
                head = head->next;
                if(!head)
                    return head;
            }
            ListNode* cur = head;
            while(cur){
                while(cur->next != NULL && cur->next->val != val)
                    cur = cur->next;
                if(!cur->next) break;
                cur->next = cur->next->next;
            }
            return head;
        }
    };
    

Log in to reply
 

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