Gamified challenge · Fast and slow pointers
A train is a singly linked list of cars (ListNodes with a number val and a link next). You get the head car train (at least one car) and a number n with 1 <= n <= the number of cars.
Write removeNth(train, n) (Python: remove_nth(train, n)) that uncouples the n-th car counted from the back (n = 1 is the last car) and returns the head of the remaining train. If the lead car is removed, the new head is the second car; if the only car is removed, return null. Try doing it in one pass.
For 1 → 2 → 3 → 4 → 5 and n = 2 return 1 → 2 → 3 → 5.
This game is part of the paid plans. Sign in, then pick a plan to write your answer, watch it play out, and step through the algorithm with its guide.
Sign in with Google