Remove all elements from a linked list of integers that have value val.
Example:
Input: 1->2->6->3->4->5->6, val = 6
Output: 1->2->3->4->5
大意:移除等于val的元素(可能不止一个)
方法:遍历链表,等于则指向val下一个,不等于则继续遍历
class Solution {
public:
ListNode* removeElements(ListNode* head, int val) {
ListNode *dummy= new ListNode(0);
ListNode *cur=dummy;
dummy->next=head;
while(cur->next){
if(cur->next->val==val){
cur->next=cur->next->next;
}
else
cur=cur->next;
}
return dummy->next;
}
};