C++ concise solution in 25ms


  • 0
    G
        ListNode* removeElements(ListNode* head, int val) {
            if (head == NULL) {
                return head;
            }
            ListNode* curr = head;
            while (curr != NULL && curr->next != NULL) {
                ListNode* temp = curr;
                while (temp->next->val == val) {
                    temp = temp->next;
                    if (temp->next == NULL) {
                        break;
                    }
                }
                curr->next = temp->next;
                curr = curr->next;
            }
            if (head->val == val) {
                return head->next;
            } else {
                return head;
            }
        }
    

Log in to reply
 

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