Remove Linked List Elements
Real-world version: Unlink Matching BinsThe statement here is our own: the same inputs, outputs and technique.
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 starts with this. It matches the parent problem's shape, so a solution that works here works on a standard judge unchanged.
def remove_elements(chain: list[int], target: int) -> list[int]:public int[] removeElements(int[] chain, int target)Checking your plan…
How this problem was checked
- Passed: Reference solution run against 10 test cases: 10 passed
- Passed: 4 worked examples confirmed by running the code
- Passed: 0 eight-word sequences shared with the parent problem's text
- Passed: Agrees with an independent brute-force solution on random inputs
- Passed: Test set shown to catch deliberately broken solutions
- Passed: Reviewed by a separate quality pass
- Passed: Function arity matches the parent problem's