题目:给你一个链表的头节点 head 和一个整数 val,请你删除链表中所有满足Node.val == val的节点,并返回新的头节点 。
struct ListNode* removeElements(struct ListNode* head, int val)
{
struct ListNode* prev = NULL;
struct ListNode* cur = head;
while (cur)//结点不为NULL就检查其val值
{
if(cur->val==val)
{
if(cur==head)//头删
{
head=cur->next;
free(cur);
cur=head;
}
else//中间删和尾删
{
prev->next=cur->next;
free(cur);
cur=prev->next;
}
}
else//不存在删除结点的情况
{
prev = cur;
cur = cur->next;
}
}
return head;
}


![[C题目]力扣203. 移除链表元素 [C题目]力扣203. 移除链表元素](http://www.mshxw.com/aiimages/31/920392.png)
