Show problems as
Classic problem #876EasyLinked ListTwo Pointers

Middle of the Linked List

Real-world version: Pump Chain Midpoint
Tracked in this browser only
Read Middle of the Linked List on LeetCode(opens in a new tab)

The statement here is our own: the same inputs, outputs and technique.

Learn the pattern firstFast and Slow Pointers · Linked List, animated step by step

A booster pipeline is a chain of pump stations. Each station knows only the pressure it runs at and which station comes next, so the chain can be travelled in one direction and its length is not recorded anywhere.

For this exercise the chain is handed to you as pressures, a flat list of the pressures in travel order, where index 0 is the station at the intake. Your answer is the same kind of flat list: the pressure of the midpoint station followed by the pressures of every station downstream of it, in travel order. That is the JSON stand-in for handing back a station and letting the rest of the chain trail behind it.

When the chain holds an even number of stations, two of them sit equally near the centre; pick the one further downstream.

Travel the chain a single time. Do not walk it once to total up the stations and then walk it again to reach the answer.

Examples

Example 1

Input
pressures = [42, 17, 63, 8, 55]
Output
[63, 8, 55]

Five stations put the centre at index 2, and the two stations downstream of it trail along behind.

Example 2

Input
pressures = [12, 30, 7, 91, 44, 6]
Output
[91, 44, 6]

Six stations leave indices 2 and 3 tied for the centre, and the tie goes to the one further downstream.

Example 3

Input
pressures = [100]
Output
[100]

A lone station is its own midpoint and has nothing trailing it.

Constraints

  • 1 <= pressures.length <= 100
  • 1 <= pressures[i] <= 100

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.

Python
def midpoint_segment(pressures: list[int]) -> list[int]:
Java
public int[] midpointSegment(int[] pressures)