Simple C++ solution using pre, cur, next


  • 0
    ListNode* removeElements(ListNode* head, int val) {
            if(head==NULL) return NULL;
            ListNode* cur=head, *next=cur->next;
            ListNode* pre=new ListNode(0);
            pre->next=head;
            ListNode* temp=pre;
            while(cur!=NULL){
                if(cur->val==val){
                    pre->next=next;
                    cur=next;
                    if(next!=NULL) next=next->next;
                }else{
                    pre=cur;
                    cur=next;
                    if(next!=NULL) next=next->next;
                }
            }
            return temp->next;
        }
    

Log in to reply
 

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