Skip to content
Decoder

Insertion Sort List

Medium

Problem

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.

Example 1

Input
4
Output
2 -> 1 -> 3 → 1 -> 2 -> 3 -> 4

Example 2

Input
-1
Output
5 -> 3 -> 4 -> 0 → -1 -> 0 -> 3 -> 4 -> 5

Constraints

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

Read it once before you touch anything

Want some pressure? Start the timer. In an interview you get about two minutes to understand the problem.

  1. 1Read the goalWhat goes in, what comes out
  2. 2Scan the examplesCheck one by hand
  3. 3Note the constraintsThey set your Big O
Say what it asks in one sentence
Say it out loud first, then compare with ours.