classSolution{public:
ListNode*removeElements(ListNode* head,int val){if(!head)return head;
ListNode * prev =newListNode(-1);
ListNode * newHead = prev;
prev -> next = head;while(head){if(head -> val == val){
prev -> next = head -> next;
head = head -> next;}else{
prev = prev -> next;
head = head -> next;}}return newHead->next;}};
方法2: recursion
classSolution{public:
ListNode*removeElements(ListNode* head,int val){if(!head)return head;
head -> next =removeElements(head -> next, val);if(head -> val == val){return head -> next;}return head;}};