← DiffPush

Delete mid of LL

Standard Bar Linked List · Medium Problems of LL O(N) · O(1)

Retiring the Middle Station of the Line

A plant retires the exact middle station of a one-way line to shorten the path. The station count isn't posted, so the locator uses the two-speed probe: when the fast probe exits, the slow probe is parked on the middle. Crews track the station immediately behind the slow probe the whole way, so the moment retirement is confirmed, that predecessor can bridge straight over the retired station.

Input: An array head of node values representing the linked list.

Output: The values of the list after deleting the middle node. For n = 1 the result is empty; with an even count, the first of the two middles is removed.

Constraints

Examples

Example 1

Input: {"head":[1,3,4,7,1,2,6]}
Output: [1,3,4,1,2,6]
Seven stations: the 4th (value 7) is the middle and gets removed.

Example 2

Input: {"head":[1,2,3,4]}
Output: [1,3,4]
Four stations have middles 2 and 3; the first one goes.

Solve this in your browser →

Also on LeetCode ↗