https://leetcode.com/problems/remove-linked-list-elements/

Input:  1->2->6->3->4->5->6,val = 6
Output: 1->2->3->4->5

recursive

class Solution {
public:
    ListNode* removeElements(ListNode* head, int val) {
        if(head == NULL) return NULL;
        ListNode* nextNode = removeElements(head->next, val);
        if(head->val == val) return nextNode;
        else{
            head->next = nextNode;
            return head;
        };
    }
};

iterative

class Solution {
public:
    ListNode* removeElements(ListNode* head, int val) {
        if(head == NULL) return NULL;
        ListNode* dummy = new ListNode(-1);
        ListNode* prev = dummy;
        prev->next = head;
        while(head)
        {
            if(head->val == val)
            {
                prev->next = head->next;
                head = head->next;
            }
            else
            {
                prev = head;
                head = head->next;
            }
        }
        return dummy->next;
    }
};