Easy to understand Code || Beats 100% || C++ || Java|| Pyhton || JavaScript
Intuition\n Describe your first thoughts on how to solve this problem. \nThe goal is to modify a linked list by removing nodes whose values are present in a giv
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nThe goal is to modify a linked list by removing nodes whose values are present in a given list (nums). The challenge lies in properly managing the linked list\'s structure when nodes need to be deleted, especially handling edge cases like deletions at the head and the last node.\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n1. **Build a Hash Map:** Use an unordered map to store the frequency of elements in the nums list. This helps in quickly checking if a node\'s value is in nums.\n\n2. **Handle Head Deletions:** First, handle deletions at the start of the list by iteratively updating the head if its value exists in nums. This ensures the head points to the first node that does not need to be removed.\n\n3. **Traverse the List:** Use two pointers, temp (current node) and prev (previous node), to traverse the linked list:\n\n    - If temp\'s value is in the hash map, adjust the pointers to remove the node and delete it.\n    - If temp\'s value is not in the hash map, simply move the prev and temp pointers forward.\n4. **Delete Nodes:** During the traversal, when a node matches a value in nums, update the previous node\'s next pointer to skip the current node (temp) and delete it.\n\n5. **Handle the Last Node:** Ensure that when you reach the end of the list, the last node is also checked and handled properly, adjusting pointers as necessary.\n\n6. **Return the Modified Head:** After processing all nodes, return the updated head of the linked list.\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n$$ O(m+n) $$\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n$$ O(m) $$\n\n```C++ []\nclass Solution {\npublic:\n    ListNode* modifiedList(vector<int>& nums, ListNode* head) {\n        if (head == NULL) {\n            return NULL;\n        }\n\n        unordered_map<int, int> mp;\n        for (int i = 0; i < nums.size(); i++) {\n            mp[nums[i]]++;\n        }\n\n        ListNode* temp = head;\n        ListNode* prev = NULL;\n\n        // Handle the case where the first node itself needs to be removed\n        while (temp != NULL && mp.find(temp->val) != mp.end()) {\n            head = temp->next;\n            delete(temp);\n            temp = head;\n        }\n\n        // Traverse and handle other nodes\n        while (temp != NULL) {\n            if (mp.find(temp->val) != mp.end()) {\n                prev->next = temp->next;\n                delete(temp);\n                temp = prev->next;\n            } else {\n                prev = temp;\n                temp = temp->next;\n            }\n        }\n\n        return head;\n    }\n};\n```\n```python []\nclass Solution:\n    def modifiedList(self, nums, head):\n        if not head:\n            return None\n\n        # Create a set for quick lookup of nums values\n        num_set = set(nums)\n\n        # Handle head deletions\n        while head and head.val in num_set:\n            head = head.next\n\n        # If the entire list is deleted\n        if not head:\n            return None\n\n        temp = head\n        prev = None\n        next_node = head.next\n\n        # Traverse the list\n        while temp:\n            if temp.val in num_set:\n                prev.next = next_node\n                temp = next_node\n                next_node = next_node.next if next_node else None\n            else:\n                prev = temp\n                temp = next_node\n                next_node = next_node.next if next_node else None\n\n        return head\n```\n```Java []\nclass Solution {\n    public ListNode modifiedList(List<Integer> nums, ListNode head) {\n        if (head == null) {\n            return null;\n        }\n\n        HashSet<Integer> set = new HashSet<>(nums);\n\n        // Handle head deletions\n        while (head != null && set.contains(head.val)) {\n            head = head.next;\n        }\n\n        // If the entire list is deleted\n        if (head == null) {\n            return null;\n        }\n\n        ListNode temp = head;\n        ListNode prev = null;\n        ListNode next = head.next;\n\n        // Traverse the list\n        while (temp != null) {\n            if (set.contains(temp.val)) {\n                prev.next = next;\n                temp = next;\n                next = (next != null) ? next.next : null;\n            } else {\n                prev = temp;\n                temp = next;\n                next = (next != null) ? next.next : null;\n            }\n        }\n\n        return head;\n    }\n}\n```\n```JavaScript []\nfunction modifiedList(nums, head) {\n  if (head === null) {\n    return null;\n  }\n\n  const numSet = new Set(nums);\n\n  // Handle head deletions\n  while (head !== null && numSet.has(head.val)) {\n    head = head.next;\n  }\n\n  // If the entire list is deleted\n  if (head === null) {\n    return null;\n  }\n\n  let temp = head;\n  let prev = null;\n  let next = head.next;\n\n  // Traverse the list\n  while (temp !== null) {\n    if (numSet.has(temp.val)) {\n      prev.next = next;\n      temp = next;\n      next = next ? next.next : null;\n    } else {\n      prev = temp;\n      temp = next;\n      next = next ? next.next : null;\n    }\n  }\n\n  return head;\n}\n```\n![upvote.png](https://assets.leetcode.com/users/images/72749936-663d-42e9-913b-f7f488b510de_1725598087.0091782.png)\n
['Array', 'Hash Table', 'Linked List', 'Python', 'C++', 'Java', 'JavaScript']