Problem

Given the head of a singly linked list, sort the list using insertion sort, and return the sorted list’s head.

The steps of the insertion sort algorithm:

  1. Insertion sort iterates, consuming one input element each repetition and growing a sorted output list.
  2. At each iteration, insertion sort removes one element from the input data, finds the location it belongs within the sorted list and inserts it there.
  3. It repeats until no input elements remain.

The following is a graphical example of the insertion sort algorithm.

More on insertion sort - Insertion Sort.

The partially sorted list (black) initially contains only the first element in the list. One element (red) is removed from the input data and inserted in-place into the sorted list with each iteration.

Similar Question

Which sorting algorithm is easily adaptable to singly linked lists?

Insertion sort is well-suited for singly linked lists. Elements are inserted by traversing the list to find the correct position or until reaching the end of the list.

Examples

Example 1:

Input: head = [4,2,1,3]
Output: [1,2,3,4]

Example 2:

Input: head = [-1,5,3,4,0]
Output: [-1,0,3,4,5]

Solution

Method 1 - Iterative

Suppose we have an list and our curr pointer is 4:

1 2 5 4 3

Our left side is already sorted, now, we have to ensure we have to find a point where we can insert the 4. So, in our insertion sort, as the algorithm progresses, left side remains sorted and we have to find the insertion point of curr pointer.

So, to break curr pointer, we need pre - the previous pointer to curr to set to next element.

Code

Java
public ListNode insertionSortList(ListNode head) {
	if( head == null ) {
		return head;
	}
	
	ListNode dummy = new ListNode(0); // new starter of the sorted list
	ListNode curr = head; // the node will be inserted
	ListNode prev = dummy; // insert node between pre and pre.next
	ListNode next = null; // the next node will be inserted
	// not the end of input list
	while(curr != null) {
		next = curr.next;
		// find the right place to insert
		while(prev.next != null && prev.next.val < curr.val) {
			prev = prev.next;
		}
		// insert between pre and pre.next
		curr.next = prev.next;
		prev.next = curr;
		prev = dummy;
		curr = next;
	}
	
	return helper.next;
}