Description:
Remove all elements from a linked list of integers that have value val.
Example
Given: 1 --> 2 --> 6 --> 3 --> 4 --> 5 --> 6, val = 6
Return: 1 --> 2 --> 3 --> 4 --> 5
算法思路:
找到符合条件的结点的前驱结点,将其前驱结点删除
struct ListNode* removeElements(struct ListNode* head, int val) { //也许不带头结点
struct ListNode *p1,*p2;
if(head==NULL)
return NULL;
p1=(struct ListNode*)malloc(sizeof(struct ListNode));
p1->next=head; //由于不带头结点,要用p1->next存储head
p2=p1;
while(p2&&p2->next)
{
if(p2->next->val==val)
{
p2->next=p2->next->next;
}
else
p2=p2->next;
}
return p1->next;
}