Skip to content

Insertion Sort List

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

You get the head of a linked list. Sort it in ascending order using insertion sort and return the new head: take nodes off the original list one at a time and insert each into its place in a growing sorted list. Relink the nodes; do not create new ones.

4 -> 2 -> 1 -> 3        →  1 -> 2 -> 3 -> 4
-1 -> 5 -> 3 -> 4 -> 0  →  -1 -> 0 -> 3 -> 4 -> 5

Constraints

  • 1 <= number of nodes <= 5000
  • -5000 <= Node.val <= 5000