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