Delete Nodes From Linked List Present in Array

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.

Example 1
[1] -> [2] -> [3] -> [4] -> [5] -> null
---
[4] -> [5] -> null
Inputnums = [1,2,3], head = [1,2,3,4,5]
Output[4,5]
Remove the nodes with values 1, 2, and 3.
Example 2
[1] -> [2] -> [1] -> [2] -> [1] -> [2] -> null
---
[2] -> [2] -> [2] -> null
Inputnums = [1], head = [1,2,1,2,1,2]
Output[2,2,2]
Remove the nodes with value 1.

Constraints

  • 1 <= nums.length <= 10^5
  • 1 <= nums[i] <= 10^5
  • All elements in nums are unique.
  • The number of nodes in the given list is in the range [1, 10^5].
  • 1 <= Node.val <= 10^5
  • The input is generated such that there is at least one node in the linked list that has a value not present in nums.

Asked at 5 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate