Show problems as
Classic problem #328MediumLinked List

Odd Even Linked List

Real-world version: Regroup the Bucket Line
Tracked in this browser only
Read Odd Even 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

An aerial ropeway carries buckets in single file. Every bucket is coupled to the one behind it and the trailing bucket is coupled to nothing, so the line can only be walked from the leading bucket backwards.

The harness passes plain JSON, so the line arrives as the array loads, holding the load readings from the leading bucket to the trailing one, and your answer takes the same shape: the readings in their new order, leading bucket first, or an empty array when the ropeway is running empty.

Number the buckets by their place in the line, starting at 1 for the leading bucket. Re-couple the line so that every odd-numbered bucket comes first and every even-numbered bucket follows, with the buckets inside each of those two groups keeping the order they already had. The numbering refers to a bucket's place in the line, not to its load reading.

Do this by re-coupling the buckets you were handed rather than hanging a second line of new buckets, and hold only a fixed number of bucket references beyond the line itself while you work.

Examples

Example 1

Input
loads = [45, 12, 60, 8, 33]
Output
[45, 60, 33, 12, 8]

Buckets 1, 3 and 5 carry 45, 60 and 33, and buckets 2 and 4 carry 12 and 8, so the odd-numbered group leads in that order and the even-numbered group follows in that order.

Example 2

Input
loads = [4, 7, 6, 9]
Output
[4, 6, 7, 9]

Buckets 1 and 3 carry 4 and 6 and buckets 2 and 4 carry 7 and 9, which is the order they appear in the answer.

Example 3

Input
loads = []
Output
[]

An empty ropeway has nothing to re-couple.

Constraints

  • 0 <= loads.length <= 10^4
  • -10^6 <= loads[i] <= 10^6
  • Only a fixed number of bucket references may be held beyond the line itself

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 regroup_bucket_line(loads: list[int]) -> list[int]:
Java
public int[] regroupBucketLine(int[] loads)