删除链表中等于给定值 val 的所有节点。
示例:
输入: 1->2->6->3->4->5->6, val = 6 输出: 1->2->3->4->5
先将其余位置节点为val的值删除,最后删除头结点
class Solution {
public:
ListNode* removeElements(ListNode* head, int val)
{
if (head == NULL)return NULL;
ListNode *p = head;
ListNode *q=head->next;
while (q)
{
if (q->val==val)
{
p->next=q->next;
}
else{p=q;}
q=q->next;
}
if(head->val==val)
{
head=head->next;
}
return head;
}
};