题目:https://leetcode-cn.com/problems/remove-linked-list-elements/description/
题解:
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
//LinkList;
struct ListNode* removeElements(struct ListNode* head, int val) {
typedef struct ListNode *Linklist;
Linklist prehead,del,cur,pre;
prehead=(Linklist)malloc(sizeof(struct ListNode));
prehead->next=head;
pre=prehead;//前驱指针
cur=head;//现在的
while(cur)//不需要判断cur的下一个是否为NULL,因为cur==NULL时循环终止
{
if(cur->val==val)
{
del=cur;
pre->next=cur->next;
cur=cur->next;
free(del);
}
else
{
pre=cur;
cur=cur->next;
}
}
return prehead->next;
}