Share my code, give some advice


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

Log in to reply
 

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