Gamified challenge · Linked lists
A train is a singly linked list of cars: each ListNode has a car number val and a coupling next to the car behind it. You get the head car (or null for an empty siding).
Write reverse(train) that turns the whole train around and returns the new head, so the cars come in the opposite order. Rewire the next couplings rather than building new cars.
For 1 → 2 → 3 → 4 → 5 return 5 → 4 → 3 → 2 → 1. An empty train stays empty, and one car stays as it is.
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