问题描述:
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
struct ListNode* removeElements(struct ListNode* head, int val) {
if(head == NULL)
{
return NULL;
}
struct ListNode* cur = head;
struct ListNode* prev = NULL;
while(cur)
{
struct LiseNode* next = cur->next;
if(cur->val == val)
{
if(prev ==NULL)
{
head = cur->next;
}
else
{
prev->next = cur->next;
}
free(cur);
cur = next;
}
else
{
prev = cur;
cur = cur->next;
}
}
return head;
}