Facebook PixelReorder List — Coding Practice
Reorder ListMedium

Reorder List

Medium 12.4k56% acceptance
Linked ListTwo PointersStack

You are given the head of a singly linked list L0 → L1 → … → Ln-1 → Ln.

Reorder the nodes to the interleaved pattern:

L0 → Ln → L1 → Ln-1 → L2 → Ln-2 → …

You must modify the list in place (rearranging the existing nodes, not their values alone) and return the head of the reordered list.

The list is given to you as a ListNode ({ val, next }); an empty list is null.

Example 1
Input: head = [1,2,3,4]
Output: [1,4,2,3]
Front and back alternate inward: 1, then 4, then 2, then 3.
Example 2
Input: head = [1,2,3,4,5]
Output: [1,5,2,4,3]
With an odd length the middle node (3) ends up last.
Example 3
Input: head = [1]
Output: [1]
Constraints
  • The number of nodes is in the range [1, 5 × 10⁴].
  • 1 ≤ Node.val ≤ 1000
Asked atAmazonMetaMicrosoftAdobe
JavaScript
Loading editor…
Case 1
[1,2,3,4]
expected: [1,4,2,3]
Case 2
[1,2,3,4,5]
expected: [1,5,2,4,3]
Case 3
[1]
expected: [1]
Case 4
[1,2]
expected: [1,2]
Case 5
[1,2,3]
expected: [1,3,2]
Case 6
[1,2,3,4,5,6]
expected: [1,6,2,5,3,4]