Beats 100% | C++ | Python  | Java | Go | Rust | JavaScript
\nEDIT1: Working on Optimization, removed the explanation\n\n# Code\n\n\nC++ []\nclass Solution {\npublic:\n    ListNode* modifiedList(vector<int>& excludeValue
\nEDIT1: Working on Optimization, removed the explanation\n\n# Code\n\n\n```C++ []\nclass Solution {\npublic:\n    ListNode* modifiedList(vector<int>& excludeValues, ListNode* head) {\n        bitset<100001> excludeSet; \n        for (int val : excludeValues) {\n            excludeSet.set(val);\n        }\n        \n        ListNode dummy(0);\n        dummy.next = head;\n        ListNode* curr = &dummy;\n        \n        while (curr->next) {\n            if (excludeSet[curr->next->val]) {\n                curr->next = curr->next->next;  \n            } else {\n                curr = curr->next;  \n            }\n        }\n        \n        return dummy.next;\n    }\n};\n\nstatic const int speedup = []() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n    cout.tie(nullptr);\n    return 0;\n}();\n\n\n\n\n```\n\n```python []\nclass Solution:\n    def modifiedList(self, excludeValues: List[int], head: Optional[ListNode]) -> Optional[ListNode]:\n        exclude_set = set(excludeValues)\n        \n        dummy = ListNode(0)\n        dummy.next = head\n        curr = dummy\n        \n        while curr.next:\n            if curr.next.val in exclude_set:\n                curr.next = curr.next.next  \n            else:\n                curr = curr.next  \n        \n        return dummy.next\n\ndef list_to_linked_list(arr):\n    dummy = ListNode(0)\n    curr = dummy\n    for val in arr:\n        curr.next = ListNode(val)\n        curr = curr.next\n    return dummy.next\n\ndef linked_list_to_list(head):\n    result = []\n    while head:\n        result.append(head.val)\n        head = head.next\n    return result\n\ndef kdsmain():\n    inputs = map(loads, sys.stdin)\n    results = []\n\n    for excludeValues in inputs:\n        head_list = next(inputs)\n        head = list_to_linked_list(head_list)\n        \n        filtered_head = Solution().modifiedList(excludeValues, head)\n        results.append(linked_list_to_list(filtered_head))\n\n    with open("user.out", "w") as f:\n        for result in results:\n            print(dumps(result).replace(", ", ","), file=f)\n\nif __name__ == "__main__":\n    kdsmain()\n    sys.exit(0)\n\n\n```\n```Java []\nclass Solution {\n    public ListNode modifiedList(int[] excludeValues, ListNode head) {\n\n        java.util.BitSet excludeSet = new java.util.BitSet();\n        for (int val : excludeValues) {\n            excludeSet.set(val);\n        }\n        ListNode dummy = new ListNode(0);\n        dummy.next = head;\n        ListNode curr = dummy;\n\n        while (curr.next != null) {\n            if (excludeSet.get(curr.next.val)) {\n                curr.next = curr.next.next;  \n            } else {\n                curr = curr.next;  \n            }\n        }\n\n        return dummy.next;\n    }\n}\n\n```\n```Go []\nfunc modifiedList(excludeValues []int, head *ListNode) *ListNode {\n    excludeSet := make(map[int]bool)\n    for _, val := range excludeValues {\n        excludeSet[val] = true\n    }\n    \n    dummy := &ListNode{Val: 0, Next: head}\n    curr := dummy\n    \n    for curr.Next != nil {\n        if excludeSet[curr.Next.Val] {\n            curr.Next = curr.Next.Next  \n        } else {\n            curr = curr.Next \n        }\n    }\n    \n    return dummy.Next\n}\n\n```\n\n```Rust []\nimpl Solution {\n    pub fn modified_list(exclude_values: Vec<i32>, head: Option<Box<ListNode>>) -> Option<Box<ListNode>> {\n        let mut exclude_set = std::collections::HashSet::new();\n        for &val in &exclude_values {\n            exclude_set.insert(val);\n        }\n        \n        let mut dummy = Box::new(ListNode::new(0));\n        dummy.next = head;\n        let mut curr = &mut dummy;\n        \n        while let Some(next) = curr.next.take() {\n            if exclude_set.contains(&next.val) {\n                curr.next = next.next;\n            } else {\n                curr.next = Some(next);\n                curr = curr.next.as_mut().unwrap();\n            }\n        }\n        \n        dummy.next\n    }\n}\n\n```\n```JavaScript []\n/**\n * @param {number[]} excludeValues\n * @param {ListNode} head\n * @return {ListNode}\n */\nvar modifiedList = function(excludeValues, head) {\n    const excludeSet = new Set(excludeValues);\n    \n    const dummy = new ListNode(0);\n    dummy.next = head;\n    let curr = dummy;\n    \n    while (curr.next) {\n        if (excludeSet.has(curr.next.val)) {\n            curr.next = curr.next.next;\n        } else {\n            curr = curr.next;  \n        }\n    }\n    \n    return dummy.next;\n};\n\n```\n
['Array', 'Linked List', 'C++', 'Java', 'Go', 'Python3', 'Rust', 'JavaScript']