struct ListNode* removeElements(struct ListNode* head, int val)
{
struct ListNode* prv = NULL;
struct ListNode* cur = head;
// 处理头节点等于val的情况
while (cur != NULL && cur->val == val)
{
head = cur->next;
free(cur);
cur = head;
}
// 处理中间节点和尾节点等于val的情况
while (cur != NULL)
{
if (cur->val == val)
{
prv->next = cur->next;
free(cur);
cur = prv->next;
}
else
{
prv = cur;
cur = cur->next;
}
}
return head;
}