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
/**
* 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) {
while(head&&head->val==val){
head=head->next;
}
if(!head){
return NULL;
}
ListNode *p1,*p2;
p1=head;
p2=head->next;
while(p2){
if(p2->val==val){
p1->next=p2->next;
p2=p2->next;
}
else{
p1=p2;
p2=p2->next;
}
}
return head;
}
};