Blind 75
21 / 75Free practice

Reorder List

Medium · Linked lists

Jump to code

Rearrange a singly linked list in place so its nodes alternate from the front and back: first, last, second, second-last, and so on. Keep node values unchanged.

Examples

Example 1

head = [1,2,3,4]

Output: [1,4,2,3]

Example 2

head = [1,2,3,4,5]

Output: [1,5,2,4,3]

Constraints

  • 1 <= Length of the list <= 1000.
  • 1 <= Node.val <= 1000

Function contract

Linked lists are displayed as value arrays. Your code receives ListNode objects with val and next fields.

Modify head in place. The grader checks its value after your function returns.

Need a hint?

Split, reverse, then weave.

Real linked list interview questions

Questions on the same topic that candidates reported from real company interviews. Some company questions are locked and open with Premium or XP.

Browse all real coding interview questions

Accepted submissions mark this problem complete. Progress stays in this browser.

Run checks 5 examples. Submit checks all 6 cases. A free account runs this problem; no XP needed.
Language
Case 1
Input
[
  [
    1,
    2,
    3,
    4
  ]
]
Expected
[
  1,
  4,
  2,
  3
]
Case 2
Input
[
  [
    1,
    2,
    3,
    4,
    5
  ]
]
Expected
[
  1,
  5,
  2,
  4,
  3
]
Case 3
Input
[
  [
    1
  ]
]
Expected
[
  1
]
Case 4
Input
[
  [
    1,
    2
  ]
]
Expected
[
  1,
  2
]
Case 5
Input
[
  [
    1,
    2,
    3
  ]
]
Expected
[
  1,
  3,
  2
]

Drafts are saved per language in this browser when storage is available.