Complete-Preparation

🎉 One-stop destination for all your technical interview Preparation 🎉

View the Project on GitHub

203_removeLinkedListElement 🌟

Given the head of a linked list and an integer val, remove all the nodes of the linked list that has Node.val == val, and return the new head.

O(N) Time and O(1) Space

Code

class Solution{
public:
    ListNode *removeElements(ListNode *head, int val){
        ListNode *curr = head;
        ListNode *prev = NULL;

        while (curr != NULL){
            if (curr->val == val){
                if (prev == NULL)
                    head = curr->next;
                else
                    prev->next = curr->next;
            }
            else{
                prev = curr;
            }
            curr = curr->next;
        }
        return head;
    }
};

O(N) Time and O(1) Space, recursive

Code

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