28ms C++ beats 99.09% cppsubmissions


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

Log in to reply
 

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