C++ O(n) time and O(1) space beats 99.38%


  • 0
    A
    ListNode* removeElements(ListNode* head, int val) 
        {
            while(head)
            {
                if(head->val != val)
                    break;
                head = head->next;
            }
            ListNode *cur = head;
            while(cur && cur->next)
            {
                while(cur->next && cur->next->val == val)
                    cur->next = cur->next->next;
                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.