Trains the technique from
LeetCode 19Remove Nth Node From End of ListThis 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.
A shunting yard models a train as a one-way coupling chain: every wagon carries a load reading and points at the wagon coupled behind it, and the rear wagon points at nothing.
The harness passes plain JSON, so the train arrives as the array wagons, holding the load readings from the front wagon back to the rear one. Your answer takes the same shape: the readings that remain, front wagon first, or an empty array when nothing is left.
Counting from the rear, where the rear wagon is number 1, unhook wagon number n and couple its two neighbours to each other. The crew has one walk down the train from the front, so rebuild the chain, then find and unhook the wagon in a single pass over the couplings while holding only a fixed number of wagon references. Measuring the train's length first and walking it again does not count as a single pass.
Example 1
From the rear, the wagon loaded with 21 is number 1 and the one loaded with 8 is number 2, so 8 rolls away and 30 couples straight to 21.
Example 2
Wagon 2 from the rear is the front wagon here, so the train now begins at the wagon loaded with 7.
Example 3
A single wagon is also the rear wagon, and unhooking it leaves an empty train.
The editor is preloaded with this. It matches the parent problem's shape, so a solution that works here transfers to a judge unchanged.
def unhook_from_tail(wagons: list[int], n: int) -> list[int]:public int[] unhookFromTail(int[] wagons, int n)See the step-by-step animation, the intuition, and clean code in every language — free, no credit card.