删除链表中等于给定值 val 的所有节点。
示例:
输入: 1->2->6->3->4->5->6, val = 6 输出: 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){
struct ListNode *head_pre = (struct ListNode *)malloc(sizeof(struct ListNode));
head_pre->next = head;
struct ListNode *p = head_pre;
while (p && p->next)
{
while (p->next && p->next->val == val)
{
if (p->next == head)
head = head->next;
else
p->next = p->next->next;
}
p = p->next;
}
return head;
}