LeetCode 203 移除链表元素

// 递归解法
class Solution {
    public ListNode removeElements(ListNode head, int val) {
        if(head == null) return null;
        ListNode tail = removeElements(head.next, val);
        if(head.val == val) return tail;
        head.next = tail;
        return head;
    }
}
// 迭代解法
class Solution {
    public ListNode removeElements(ListNode head, int val) {
        ListNode hh = new ListNode(0, head), pre = hh;
        while(pre.next != null) {
            if(pre.next.val == val) pre.next = pre.next.next;
            else pre = pre.next; 
        }
        return hh.next;
    }
}