Simple Explanation and easy solution
Intuition\n Describe your first thoughts on how to solve this problem. \n\nThe problem involves modifying a linked list by removing nodes that contain values fo
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\nThe problem involves modifying a linked list by removing nodes that contain values found in a given vector nums. The goal is to traverse the linked list and eliminate any node whose value is present in nums, ultimately returning the modified list with those nodes removed.\n\nThe approach leverages a hash map (or unordered map) to efficiently track which values should be removed. This allows for quick lookups when traversing the linked list, ensuring that we can determine in constant time whether a node\'s value needs to be deleted. The algorithm works by maintaining two pointers, curr (for traversal) and prev (to adjust the links as we remove nodes). If the current node\'s value is found in nums, we unlink it from the list, making sure to handle the case where the node to be deleted is at the head. Finally, we return the modified list with all unwanted nodes removed.\n\nThis approach provides a linear-time solution by combining an efficient lookup structure (the hash map) with a single pass through the linked list, making it scalable for larger inputs.\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\nInput Description:\n\nnums: A vector of integers that contains the values to be removed from the linked list.\nhead: The head pointer to a singly linked list.\nStep-by-Step Explanation:\n\n**Step 1: Populate a Hash Map (st)**\n\nFirst, we iterate through the nums vector and store the count of each element in an unordered map st. This map helps us quickly check whether a value exists in the nums vector (which indicates that the corresponding node should be deleted from the linked list).\n\n**Step 2: Initialize Pointers**\n\nWe initialize two pointers, curr and prev, both starting at the head of the linked list:\ncurr: This pointer is used to traverse the linked list.\nprev: This pointer keeps track of the previous node as we move through the list, helping to unlink the current node if necessary.\n\n**Step 3: Traverse the Linked List**\n\nWe loop through the list while curr is not nullptr.\nFor each node, we check if the value of curr exists in the hash map (st):\nIf st[curr->val] > 0 (i.e., the current node\'s value is in nums), we delete the node:\nIf prev is equal to curr, it means we are at the head of the list. We update the head pointer to skip the current node (head = head->next) and advance both curr and prev.\nIf prev is not equal to curr, we skip the current node by linking prev->next to curr->next and advancing curr while keeping prev unchanged.\nIf curr->val is not in the hash map, we simply move both prev and curr forward.\n\n**Step 4: Return the Modified List**\n\nAfter completing the traversal, we return the modified head pointer, which now points to the updated linked list with the specified values removed.\n\n**Edge Cases**:\n\nIf nums is empty, the list remains unchanged.\nIf all elements of the list are in nums, the entire list will be deleted, and nullptr will be returned.\n\n# Complexity\n- Time complexity:O(N+M);\n\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:O(M);\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```cpp []\n/**\n * Definition for singly-linked list.\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 */\nclass Solution {\npublic:\n    ListNode* modifiedList(vector<int>& nums, ListNode* head) {\n        unordered_map<int,int>st;\n        for(auto it : nums){\n            st[it]++;\n        }\n        ListNode* curr=head;\n        ListNode* prev=head;\n        while(curr!=nullptr){\n            if(st[curr->val]>0){\n                if(prev==curr){\n                    if(head==curr){\n                        head=head->next;\n                    }\n                    curr=curr->next;\n                    prev=curr;\n                }\n                else{\n                    curr=curr->next;\n                    prev->next=curr;\n                }\n            }\n            else{\n                prev=curr;\n                curr=curr->next;\n            }\n        }\n        return head;\n    }\n};\n```
['Array', 'Linked List', 'C++']