You are given an array of integers nums and the head of a linked list. Return the head of the modified linked list after removing all nodes from the linked list that have a value that exists in nums.
classSolution:
defmodifiedList(
self, nums: List[int], head: Optional[ListNode]
) -> Optional[ListNode]:
# Convert nums list to set for O(1) lookup times. num_set = set(nums)
# Initialize a dummy node to handle edge cases easily. dummy = ListNode(0)
dummy.next = head
current = dummy
# Traverse the linked list.while current and current.next:
if current.next.val in num_set:
current.next = current.next.next # Remove the node.else:
current = current.next
return dummy.next # Return the new head.