题目描述:
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
程序:
class Solution {
public:
ListNode* removeElements(ListNode* head, int val) {
while(head && head->val == val){
head = head->next;
}
if(head == NULL) return NULL;
ListNode *pre, *cur;
pre = head;
cur = head->next;
while(cur){
if(cur->val == val){
pre->next = cur->next;
cur = pre->next;
}
else
{
pre = cur;
cur = cur->next;
}
}
return head;
}
};