Simple solution in C++


  • 0
    H
        ListNode* removeElements(ListNode* head, int val) {
            ListNode *p = head;
            ListNode dummy(0);
            ListNode *n =&dummy;
            while(p != NULL)
            {
                if(p->val != val)
                {
                    n->next = p;
                    n = n->next;
                }
                p = p->next;
            }
            n->next = NULL;
            return dummy.next;
        }
    

Log in to reply
 

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