🌟| Efficient Solution || 229ms Beats 99.99% | O(n) | C++ Py3 Java |
\n\n---\n\n\uD83C\uDF1F\n\n# Intuition & Approach\nWe need to remove all nodes from the linked list whose values are present in the given array nums. We can ach
\n\n---\n\n\uD83C\uDF1F\n\n# Intuition & Approach\nWe need to remove all nodes from the linked list whose values are present in the given array **nums**. We can achieve this efficiently by using a **bitset** to track the values that need to be removed, ensuring constant time checks. We initialize a dummy node to simplify the list manipulation and iterate through the list, bypassing nodes that are in the **bitset**. This method is efficient and leverages the bitset for fast lookups.\n\n# Complexity\n- **Time Complexity** $$O(n)$$, where $$n$$ is the number of nodes in the linked list, as we traverse the list once.\n- **Space Complexity** $$O(m)$$, where $$m$$ is the number of elements in **nums**, due to the space required for the bitset.\n\n---\n\n```cpp []\n#include <vector>\n#include <bitset>\nusing namespace std;\n\nstruct ListNode {\n    int val;\n    ListNode *next;\n    ListNode(int x) : val(x), next(nullptr) {}\n};\n\nclass Solution {\npublic:\n    ListNode* modifiedList(vector<int>& nums, ListNode* head) {\n        bitset<100001> filter;\n        for (int num : nums) filter[num] = 1;\n        ListNode dummy(0); dummy.next = head;\n        for (ListNode* node = &dummy; node->next;)\n            filter[node->next->val] ? node->next = node->next->next : node = node->next;\n        return dummy.next;\n    }\n};\nstatic const int speedup = []() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n    return 0;\n}();\n```\n\n```python []\nclass ListNode:\n    def __init__(self, x):\n        self.val = x\n        self.next = None\nclass Solution:\n    def modifiedList(self, nums, head):\n        to_remove = set(nums)\n        dummy = ListNode(0)\n        dummy.next = head\n        current = dummy\n        while current.next:\n            if current.next.val in to_remove:\n                current.next = current.next.next\n            else:\n                current = current.next\n        return dummy.next\n```\n\n```java []\nimport java.util.HashSet;\nimport java.util.Set;\nclass ListNode {\n    int val;\n    ListNode next;\n    ListNode(int x) { val = x; }\n}\nclass Solution {\n    public ListNode modifiedList(int[] nums, ListNode head) {\n        Set<Integer> toRemove = new HashSet<>();\n        for (int num : nums) toRemove.add(num);\n\n        ListNode dummy = new ListNode(0);\n        dummy.next = head;\n        ListNode current = dummy;\n        while (current.next != null) {\n            if (toRemove.contains(current.next.val)) {\n                current.next = current.next.next;\n            } else {\n                current = current.next;\n            }\n        }\n        return dummy.next;\n    }\n}\n```\n\n- \n![image.png](https://assets.leetcode.com/users/images/fda2deb1-905e-40ce-9a95-8caa222ce490_1725593640.6640737.png)\n---\n
['C++', 'Java', 'Python3']