FastPrepOdd-Even Linked List

Odd-Even Linked List

Bloomberg LP logoBloomberg LP● MediumNEW GRADONSITE INTERVIEW
Learn

Problem statement

A singly linked list is serialized by the array values, from head to tail. Reorder the existing nodes so every node at an odd one-based position appears first, followed by every node at an even one-based position.

Preserve the original relative order within both position groups and return the reordered values from head to tail. The first node is at position 1.

Function

oddEvenLinkedList(values: int[]) → int[]

Examples

Example 1

values = [1,2,3,4,5]return = [1,3,5,2,4]

Positions 1, 3, and 5 stay in order before positions 2 and 4.

Example 2

values = [2,1,3,5,6,4,7]return = [2,3,6,7,1,5,4]

Grouping depends on node position, not whether a stored value is odd or even.

Example 3

values = []return = []

An empty list remains empty.

Constraints

  • 0 <= values.length <= 100000.
  • -10^9 <= values[i] <= 10^9.

More Bloomberg LP problems

See Bloomberg LP hiring insights
public int[] oddEvenLinkedList(int[] values) {
    // Write your code here.
}
values[1,2,3,4,5]
expected[1,3,5,2,4]
Checking account…