递归其实可以写成固定的三段式
/**
* 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){
return head;
}
head->next = removeElements(head->next,val);
return head->val == val ?head->next :head;
}
};