203.移除链表元素

/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode() : val(0), next(nullptr) {}
* ListNode(int x) : val(x), next(nullptr) {}
* ListNode(int x, ListNode *next) : val(x), next(next) {}
* };
*/
class Solution {
public:
ListNode* removeElements(ListNode* head, int val) {
ListNode *t=new ListNode(0);//虚拟头结点(有data+指针)
t->next=head;
ListNode *p=t;
while(p->next!=nullptr)
{
if(p->next->val==val)
p->next=p->next->next;
else
p=p->next;
}
return t->next;
}
};