Java | Python Solution
Intuition\n Describe your first thoughts on how to solve this problem. \nThe problem asks to modify a linked list by removing nodes that contain specific values
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nThe problem asks to modify a linked list by removing nodes that contain specific values. The values to be removed are provided in an array. The goal is to efficiently remove nodes from the list while preserving the order of the remaining nodes.\n\nTo achieve this, we can use a set data structure to store the values to be removed. The set allows for **O(1)** lookups, which makes it efficient to check whether a node\'s value should be removed. We traverse the linked list, and for each node, if its value exists in the set, we remove the node by updating pointers. Otherwise, we move to the next node.\n# Approach\n<!-- Describe your approach to solving the problem. -->\n1. Create a dummy node: This is a common technique used when modifying linked lists. The dummy node is placed before the head to handle edge cases like removing the head node without additional checks.\n\n2. Populate the set: Convert the array nums to a set to allow constant-time lookups for values that need to be removed from the linked list.\n\n3. Iterate through the list:\n\n    - Use a pointer **ptr** that starts at the dummy node.\n    - For each node, check if its value exists in the set.\n    - If the value is in the set, update ptr.next to skip the current node, effectively removing it from the list.\n    - If the value is not in the set, move ptr to the next node.\n    4. Return the modified list: The dummy node\'s next pointer will now point to the modified list\n# Complexity\n- Time complexity: **O(n + m)**\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: **O(n)**\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```java []\n/**\n * Definition for singly-linked list.\n * public class ListNode {\n *     int val;\n *     ListNode next;\n *     ListNode() {}\n *     ListNode(int val) { this.val = val; }\n *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }\n * }\n */\nclass Solution {\n    public ListNode modifiedList(int[] nums, ListNode head) {\n        ListNode dummy = new ListNode();\n        dummy.next = head;\n        ListNode ptr = dummy;\n        Set<Integer> set = new HashSet<>();\n        for (int num : nums){\n            set.add(num);\n        }\n        while (ptr != null && ptr.next != null){\n            if (set.contains(ptr.next.val)){\n                ptr.next = ptr.next.next;\n            } else {\n                ptr = ptr.next;\n            }\n        }\n        return dummy.next;\n    }\n}\n```\n```Python []\n# Definition for singly-linked list.\n# class ListNode:\n#     def __init__(self, val=0, next=None):\n#         self.val = val\n#         self.next = next\nclass Solution:\n    def modifiedList(self, nums: List[int], head: Optional[ListNode]) -> Optional[ListNode]:\n        dummy = ListNode()\n        dummy.next = head\n        ptr = dummy\n        nums = set(nums)\n        while ptr and ptr.next:\n            if ptr.next.val in nums:\n                ptr.next = ptr.next.next\n            else:\n                ptr = ptr.next\n        return dummy.next\n```
['Array', 'Linked List', 'Python', 'Java', 'Python3']