原题:
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
代码如下:
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
struct ListNode* removeElements(struct ListNode* head, int val) {
if(head==NULL)
return head;
while(head!=NULL&&head->val==val)
{
head=head->next;
}
struct ListNode* phead;
phead=head;
while(phead&&phead->next!=NULL)
{
if(phead->next->val==val)
{
phead->next=phead->next->next;
}
else
{
phead=phead->next;
}
}
return head;
}
这个就不说了。