C solution.( with free( ) )


  • 1
    N
    struct ListNode* removeElements(struct ListNode* head, int val) {
        struct ListNode *p=NULL,*del=NULL,*pre;
        for(p=head;p!=NULL;p=p->next){
            if(p->val==val){
                if(p==head){
                    del=head;
                    head=head->next;
                }else{
                    del=p;
                    pre->next=p->next;
                }
                free(del);
            }else{
                pre=p;   
            }    
        }
        return head;
    }
    

Log in to reply
 

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