My C++ AC Solution


  • 0
    A
    class Solution {
    public:
        ListNode* removeElements(ListNode* head, int val) {
          
            if(!head)
                return head;
                
           
            while(head && head->val == val) //removing vals in beginning of list
                head = head->next;
                
             ListNode* res = head;//first non val value
            
            while(head && head->next)
            {
                if(head->next->val == val)
                {
                    head->next = head->next->next;
                    continue;
                }
                
                head = head->next;
            }
          
            return res;
        }
    };

Log in to reply
 

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