Reorder List

Linked Lists, problem 5 of 8

Reorder List

Medium

LC #143

fast & slow pointersin-place reversalmerge

Not attempted yet

Given the head of a list L0 → L1 → … → Ln-1 → Ln, rearrange the nodes in place into:

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

Don't change node values, only the links. The function returns nothing; the test checks the list starting at head afterwards.

Example 1

Input: head = [1,2,3,4]
Output: [1,4,2,3]

Example 2

Input: head = [1,2,3,4,5]
Output: [1,5,2,4,3]

Example 3

Input: head = [10,20,30]
Output: [10,30,20]

Constraints

  • 1 ≤ number of nodes ≤ 5 · 10⁴
  • 1 ≤ Node.val ≤ 10⁵

Python

Loading draft…

Test results

7 tests available

No results yet

Run tests your code against the examples; Submit runs the hidden tests too.

3 examples, 4 hidden

Run examples, then submit all tests.