find runtime error in remove link list element

struct ListNode* removeElements(struct ListNode* head, int val){
struct ListNode* ptr=head; struct ListNode* temp;

      while(ptr!=NULL && ptr->val==val)
      {
          head=NULL;
          free(ptr);
         
      }
      
      
          while(ptr!=NULL)
          {
          while(ptr->next!=NULL && ptr->val!=val)
          {
              temp=ptr;
              ptr=ptr->next;
          
          }
              if(ptr==NULL)
                  return;
          if(ptr->val==val)
          {
              temp->next=ptr->next;
              free(ptr);
          }
              ptr=temp->next;
          }
           
return head;

}

Comments (0)