移除链表元素
/**
- Definition for singly-linked list.
- struct ListNode {
-
int val;
-
ListNode *next;
-
ListNode(int x) : val(x), next(NULL) {}
- };
/
class Solution {
public:
ListNode removeElements(ListNode* head, int val) {
//使用一个头结点为了让head的操作和其他结点一样,不用特殊处理的
ListNode* new_head=new ListNode(-1);
ListNode* pre=new_head;
new_head->next=head;
while(pre->next!=NULL)
{
if(pre->next->val==val)
{
pre->next=pre->next->next;
}
else
pre=pre->next;
}
return new_head->next;
}
};