All problems
0389EasyLinked ListRecursion

Unlink Matching Bins

Tracked in this browser only
Write code

Trains the technique from

LeetCode 203Remove Linked List Elements

This is an original problem, written from a brief that listed the technique, the difficulty, the topics, the function shape and the input bounds — none of that problem's wording, examples, hints or editorials. The link is there so you can map your practice onto the standard set.

Same function shape, different story and different numbers.

Parts bins travel down a line hooked together in a single chain, each bin holding a hook to the one behind it. The chain is given to you as chain, the list of bin labels from the front hook to the last bin; an empty list means the line is running empty.

Every bin whose label equals target has to come off the line. Take each one out by re-hooking its predecessor to its successor, so the bins that stay keep their order and the rest of the chain is untouched.

Return the labels of the chain that is left, again from front to back, or an empty list if no bin survives. Solve it by walking the chain and moving hooks, not by filtering the list with a library call, and remember that the very first bin may also have to come off.

Examples

Example 1

Input
chain = [1, 7, 7, 2], target = 7
Output
[1, 2]

Both middle bins carry label 7, so bin 1 is re-hooked straight to bin 2.

Example 2

Input
chain = [7, 7, 3], target = 7
Output
[3]

The two bins at the front both come off, and the front hook moves to bin 3.

Example 3

Input
chain = [1, 2, 3], target = 9
Output
[1, 2, 3]

No bin carries label 9, so the chain comes back unchanged.

Example 4

Input
chain = [4, 4, 4], target = 4
Output
[]

Every bin matches, so the line ends up empty.

Constraints

  • 0 <= chain.length <= 10^4
  • 1 <= chain[i] <= 50
  • 0 <= target <= 50

The signature

The editor is preloaded with this. It matches the parent problem's shape, so a solution that works here transfers to a judge unchanged.

Python
def remove_elements(chain: list[int], target: int) -> list[int]:
Java
public int[] removeElements(int[] chain, int target)
September 7
Apply