C++ Solution || Beats 92% || Easy For Beginners
Intuition\nWe know that we can traverse the linked list by having 2 pointers, prev and current, and continuining until the current pointer is pointing to NULL. 
# Intuition\nWe know that we can traverse the linked list by having 2 pointers, prev and current, and continuining until the current pointer is pointing to NULL. Now, we somehow have to traverse the list whilst also checking each element of the nums array. \n\nWe also need to delete and update the head pointer if the value is the same as that of the array nums. Note that we need to consider the edgecase where head is also to be deleted.\n\n# Approach\n1. **Use a Hash Set for Fast Lookups:**\nFirst, convert the nums array into a hash set. This allows for constant-time lookup (O(1)) when checking if a node\'s value needs to be removed, instead of looping through the array each time.\n\n2. **Create a Dummy Node:**\nTo handle edge cases like when the head node needs to be removed, we introduce a dummy node. This dummy node points to the original head of the list, simplifying the removal logic, especially for the head.\n\n3. **Traverse the Linked List:**\nUsing two pointers, prev and curr, we traverse the linked list:\n    curr points to the current node being examined.\n    prev points to the last node that wasn\'t removed.\n4. **Remove Matching Nodes:**\n    For each node:\n        - If curr->val exists in the set (i.e., the node\'s value is in nums), we remove it by updating prev->next to skip over curr.\n        - If curr->val is not in the set, we simply move prev forward to curr.\n\n5. **Free Memory (Optional):**\nIf memory management is a concern, delete the removed nodes to free memory. Not required for this question.\n\n6. **Return the New Head:**\nAfter traversing and modifying the list, return dummy->next as the new head, which handles the case where the head might have been removed.\n\n# Complexity\n![image.png](https://assets.leetcode.com/users/images/39bde1d9-ba34-467a-9f1a-f11375abc515_1725640199.6501102.png)\n\n- Time complexity: $$O(n)$$\n\n- Space complexity: $$O(n)$$\n\n# Code\n```cpp []\n\n// Definition for singly-linked list. (Given in question)\n\n// struct ListNode {\n// int val;\n// ListNode *next;\n// ListNode() : val(0), next(nullptr) {}\n// ListNode(int x) : val(x), next(nullptr) {}\n// ListNode(int x, ListNode *next) : val(x), next(next) {}\n// };\n\n\nclass Solution {\npublic:\n    ListNode* modifiedList(vector<int>& nums, ListNode* head) {\n        \n        // Unordered set for the complexity of 1.\n        // Will have to handle edge case where head is stored in num\n        unordered_set<int> removables (nums.begin(), nums.end());\n        ListNode *edgeCase = new ListNode(0); \n        edgeCase->next = head;\n        ListNode *prev = edgeCase;\n        ListNode *curr = head; \n        while (curr != NULL) {\n            if (removables.count(curr->val)){\n                prev->next = curr->next;\n                // delete curr; \n                \n            }\n            else prev = curr;\n            curr = prev->next;\n        }\n        return edgeCase->next;\n    }\n};\n```
['C++']