Skip to content

Sort List

MediumLinked List
Decode it first. Clues, constraints, pattern and plan before any code.

You get the head of a linked list. Return the list sorted in ascending order. Aim for O(n log n) time, and relink the nodes instead of copying values into an array.

4 -> 2 -> 1 -> 3        →  1 -> 2 -> 3 -> 4
-1 -> 5 -> 3 -> 4 -> 0  →  -1 -> 0 -> 3 -> 4 -> 5
(empty)                 →  (empty)

Constraints

  • 0 <= number of nodes <= 5 * 10^4
  • -10^5 <= Node.val <= 10^5