Straightforward Approach || C++
Complexity\n- Time complexity: O(nlogn + mlogn) where n is the size of nums and m is the number of nodes in the linked list.\n\n- Space complexity: O(n)\n\n# Co
# Complexity\n- Time complexity: $$O(nlogn + mlogn)$$ where n is the size of nums and m is the number of nodes in the linked list.\n\n- Space complexity: $$O(n)$$\n\n# Code\n```cpp []\nclass Solution {\npublic:\n    ListNode* modifiedList(vector<int>& nums, ListNode* head) {\n        // Convert the array `nums` into a set for fast lookup of values to be removed.\n        set<int> st(nums.begin(), nums.end());\n\n        // Initialize a pointer `curr` to traverse the linked list.\n        ListNode* curr = head;\n        \n        // Traverse the list to find the first node whose value is NOT in the set.\n        // This node will be the new head of the modified linked list.\n        while(curr) {\n            if(st.count(curr->val)) { \n                // If the current node\'s value is in the set, skip this node.\n                curr = curr->next;\n            }\n            else break; // Stop when we find a node whose value is NOT in the set.\n        }\n\n        // The first valid node becomes the new head of the modified list.\n        ListNode* newHead = curr;\n\n        // Traverse the rest of the linked list and remove nodes whose values exist in the set.\n        while(curr && curr->next) { \n            if(st.count(curr->next->val)) {\n                // If the next node\'s value is in the set, skip the next node by adjusting pointers.\n                curr->next = curr->next->next;\n            } \n            else {\n                // Move to the next node if it\'s not in the set.\n                curr = curr->next;\n            }\n        }\n\n        // Return the new head of the modified linked list.\n        return newHead;\n    }\n};\n\n```\n# Same idea, but implemented in a single pass with memory deallocation\n```cpp []\nListNode* modifiedList(vector<int>& nums, ListNode* head) {\n    set<int> st(nums.begin(), nums.end());\n    ListNode* temp = new ListNode(0);\n    temp->next = head;\n    ListNode* newHead = temp;\n\n    while(temp->next){\n        if(st.count(temp->next->val)){\n            ListNode* d = temp->next;\n            temp->next = temp->next->next;\n            delete(d);\n        }\n        else{\n            temp = temp->next;\n        }\n    }\n    return newHead->next;\n}\n```\n# Recursive Method\n```cpp []\nListNode* f(ListNode* head, set<int> &st){\n    if(! head) return NULL;\n    head->next = f(head->next, st);\n    if(st.count(head->val)){\n        ListNode* next = head->next;\n        delete(head);\n        return next;\n    }\n    return head;\n}\n\nListNode* modifiedList(vector<int>& nums, ListNode* head) {\n    set<int> st(nums.begin(), nums.end());\n    return f(head, st);\n}\n```
['C++']