Ctrl K

Purge Banned Values

Medium
You get an array banned of distinct forbidden numbers and the head head of a singly linked list. Remove every node whose value appears in banned and return the head of the resulting list. The remaining nodes must keep their original order. At least one node is guaranteed to survive.
Example 1
banned=[4, 9], head=list_node([9, 2, 4, 4, 7]) → [2, 7]

All nodes holding 9 or 4 are removed; the rest keep their order: 2, 7.

Example 2
banned=[6], head=list_node([1, 3, 5]) → [1, 3, 5]

There are no 6s in the list, so it stays the same.

Example 3
banned=[1, 2], head=list_node([2, 8, 1, 8]) → [8, 8]
Constraints
  • 1 ≤ len(banned) ≤ 10⁵
  • 1 ≤ banned[i] ≤ 10⁵, all distinct
  • The list has between 1 and 10⁵ nodes
  • 1 ≤ node value ≤ 10⁵
  • At least one node's value is not in banned
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsMarked Runs in a Chain MediumSudoku Board Without Clashes MediumAnagram Buckets MediumZero Out the Crosses MediumTree From Pre-Order and In-Order Walks Medium
loading Python…
Input
banned=[4, 9], head=list_node([9, 2, 4, 4, 7])
Expected
[2, 7]

Run checks the first 3 tests in your browser. Submit checks all 81 tests on the server, hidden ones included.