An easy understanding C solution with 13 ms


  • 0
    K
      struct ListNode* removeElements(struct ListNode* head, int val) {
            struct ListNode *p,*temp;
            p=head;
            while(p!=NULL&&p->val==val)
            {
                 head=p->next;
                 free(p);
                     p=head;
                }
                if(head==NULL) return NULL;
               while(p->next!=NULL)
               {
                   if(p->next->val!=val)
                   p=p->next;
                   else
                   {
                       temp=p->next;
                       p->next=temp->next;
                       free(temp);
                   }
               }
               return head;
            }

Log in to reply
 

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