Insertion Sort List - Problem
Sort a Singly Linked List Using Insertion Sort
Given the
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.
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
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
โ Quadratic Growth
Space Complexity
O(1)
Only uses a constant amount of extra space for pointers
โ 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
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code