5 line iterative solution

  • 0
    ListNode* removeElements(ListNode* head, int val) {
            if(!head)   return head;
            while(head and head->val==val)   head = head->next;
            ListNode *temp = head;
                ListNode *curr = temp->next;
                while(curr and curr->val==val)   curr = curr->next;
                temp->next = curr;
                temp = curr;
            return head;

Log in to reply

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