simple and easy to understand C++ solution


  • 0
    K
    class Solution {
    public:
        ListNode* removeElements(ListNode* head, int val) {
            if(!head) return head;
            ListNode* head1 = head;
            while(head->next) {
                if(head->next->val == val)
                    head->next = head->next->next;
                else
                    head = head->next;
            }
            return (head1->val != val? head1:head1->next);
        }
    };
    

Log in to reply
 

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