Show problems as
Classic problem #3217MediumArrayHash TableLinked List

Delete Nodes From Linked List Present in Array

Real-world version: Dropping the Banned Tags From the Chain
Tracked in this browser only
Read Delete Nodes From Linked List Present in Array on LeetCode(opens in a new tab)

The statement here is our own: the same inputs, outputs and technique.

A run of codes banned is given, no two of them the same, and a chain of tags follows.

Because the harness passes plain JSON, the chain reaches you as chain, listing the tag codes in order from the front.

Drop every tag whose code turns up in banned and return what is left, again as a plain list read from the front. At least one tag always survives.

Examples

Example 1

Input
banned = [1, 3, 5], chain = [1, 2, 3, 4, 5, 6]
Output
[2, 4, 6]

The three odd codes are banned, so the three even tags survive in the order they stood.

Example 2

Input
banned = [7], chain = [7, 1, 7, 2, 7, 3]
Output
[1, 2, 3]

Every tag coded 7 goes, whichever position it held, and the others keep their order.

Example 3

Input
banned = [6], chain = [1, 2, 3, 4, 5]
Output
[1, 2, 3, 4, 5]

No tag carries the banned code, so the chain comes back untouched.

Constraints

  • 1 <= banned.length <= 10^5
  • 1 <= banned[i] <= 10^5
  • no two codes in banned are the same
  • 1 <= chain.length <= 10^5
  • 1 <= chain[i] <= 10^5
  • at least one tag carries a code missing from banned

The signature

The editor starts with this. It matches the parent problem's shape, so a solution that works here works on a standard judge unchanged.

Python
def modified_list(banned: list[int], chain: list[int]) -> list[int]:
Java
public int[] modifiedList(int[] banned, int[] chain)