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