Middle of the Linked List — Fast & Slow Pointers

Find the central item of a chain, preferring the later item if there is a tie in the middle, and return everything from there on.

Fast & Slow Pointers · Easy

The problem

The carts of a baggage train are coupled one behind the other, and you hold the front cart (head). Return the cart at the centre of the train. If the train has two centre carts, return the second of them.

The story

A baggage train of 1 to 600 carts is coupled one behind another at an airport, and you're at the front cart. The ground crew wants to hang the transfer sign on the centre cart. Find it; if there are two centre carts, pick the second one. The cart you hand back comes with everything coupled behind it.

What to return

Constraints

Example

Input
head = 7→3→9→4→8
Output
9→4→8

Solve it

More in Fast & Slow Pointers

All algorithm problems