Insertion Sort List - Problem
Sort a Singly Linked List Using Insertion Sort

Given the head of a singly linked list, your task is to sort the list using the insertion sort algorithm and return the head of the sorted list.

How insertion sort works:
๐Ÿ”„ Iterative process: Takes one element at a time from the unsorted portion
๐ŸŽฏ Find position: Locates the correct position in the already sorted portion
๐Ÿ”— Insert in place: Places the element at its correct position
โ™ป๏ธ Repeat: Continues until all elements are processed

The challenge here is implementing this classic sorting algorithm on a linked list instead of an array, which requires careful pointer manipulation to maintain the list structure while sorting.

Input & Output

example_1.py โ€” Standard Case
$ Input: head = [4,2,1,3]
โ€บ Output: [1,2,3,4]
๐Ÿ’ก Note: The linked list 4โ†’2โ†’1โ†’3 is sorted using insertion sort. Each element is taken from the unsorted portion and inserted into its correct position in the sorted portion, resulting in 1โ†’2โ†’3โ†’4.
example_2.py โ€” Already Sorted
$ Input: head = [1,2,3,4]
โ€บ Output: [1,2,3,4]
๐Ÿ’ก Note: The linked list is already sorted, so insertion sort maintains the same order. This is the best case scenario for insertion sort with O(n) time complexity.
example_3.py โ€” Single Node
$ Input: head = [42]
โ€บ Output: [42]
๐Ÿ’ก Note: A single node is already sorted by definition. The algorithm handles this edge case by returning the same node.

Visualization

Tap to expand
Aโ™ 14โ™ฃ4Sorted Hand3โ™ฆ32โ™ฅ25โ™ 5Unsorted Pile3โ™ฆ3Insert between 1 and 4Aโ™ 13โ™ฆ34โ™ฃ4Result: SortedAlgorithm Steps:1. Keep sorted cards on left2. Take next card from pile3. Find correct position4. Slide card into place5. Repeat until doneTime: O(nยฒ)Space: O(1)
Understanding the Visualization
1
Pick Next Card
Take the next unsorted card from the pile
2
Find Position
Compare with sorted cards to find where it belongs
3
Slide Into Place
Insert the card at its correct position
4
Repeat
Continue until all cards are sorted
Key Takeaway
๐ŸŽฏ Key Insight: Insertion sort works by maintaining two portions - sorted (left) and unsorted (right). For each element, we find its correct position in the sorted portion and insert it there, gradually growing the sorted portion until the entire list is sorted.

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(nยฒ)

For each of n nodes, we may need to traverse up to n nodes to find insertion position

n
2n
โš  Quadratic Growth
Space Complexity
O(1)

Only uses a constant amount of extra space for pointers

n
2n
โœ“ Linear Space

Constraints

  • The number of nodes in the list is in the range [0, 5000]
  • -5000 โ‰ค Node.val โ‰ค 5000
  • The input is a singly linked list (no backward pointers)
  • You must sort the list in-place using insertion sort algorithm
Asked in
Google 15 Amazon 12 Microsoft 8 Apple 6
52.0K Views
Medium Frequency
~25 min Avg. Time
1.8K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen