My simple solution


  • 0
    V
    class Solution {
    public:
        ListNode* removeElements(ListNode* head, int val) {
            auto dummy = new ListNode( -1 );
            dummy->next = head;
        
            ListNode *left = dummy;
            ListNode *right = head;
            while( right )
            {
                if( right->val == val )
                    left->next = right->next;
                else
                    left = right;
            
                right = right->next;
            }
            return dummy->next;
        }
    };

Log in to reply
 

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