304 North Cardinal St.
Dorchester Center, MA 02124

# Insertion Sort List LeetCode Solution

## Problem – Insertion Sort List

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. 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.

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]``````

Constraints:

• The number of nodes in the list is in the range `[1, 5000]`.
• `-5000 <= Node.val <= 5000`

### Insertion Sort List LeetCode Solution in Java

``````    public ListNode insertionSortList(ListNode ptr) {
if (ptr == null || ptr.next == null)
return ptr;

ListNode preInsert, toInsert, dummyHead = new ListNode(0);

while (ptr != null && ptr.next != null) {
if (ptr.val <= ptr.next.val) {
ptr = ptr.next;
} else {
toInsert = ptr.next;
// Locate preInsert.
while (preInsert.next.val < toInsert.val) {
preInsert = preInsert.next;
}
ptr.next = toInsert.next;
// Insert toInsert after preInsert.
toInsert.next = preInsert.next;
preInsert.next = toInsert;
}
}

}
``````

### Insertion Sort List LeetCode Solution in Python

``````     def insertionSortList(self, head):

# Locate nodeToInsert.
# Locate nodeToInsertPre.
while nodeToInsertPre.next.val < nodeToInsert.val:
nodeToInsertPre = nodeToInsertPre.next

# Insert nodeToInsert between nodeToInsertPre and nodeToInsertPre.next.
nodeToInsert.next = nodeToInsertPre.next
nodeToInsertPre.next = nodeToInsert
else:

``````

### Insertion Sort List LeetCode Solution in C++

``````/**
* struct ListNode {
*     int val;
*     ListNode *next;
*     ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* dummy = new ListNode(0);
ListNode *pre = dummy, *cur = head;
while (cur) {
if ((cur -> next) && (cur -> next -> val < cur -> val)) {
while ((pre -> next) && (pre -> next -> val < cur -> next -> val)) {
pre = pre -> next;
}
ListNode* temp = pre -> next;
pre -> next = cur -> next;
cur -> next = cur -> next -> next;
pre -> next -> next = temp;
pre = dummy;
}
else {
cur = cur -> next;
}
}
return dummy -> next;
}
};
``````
##### Insertion Sort List LeetCode Solution Review:

In our experience, we suggest you solve this Insertion Sort List LeetCode Solution and gain some new skills from Professionals completely free and we assure you will be worth it.

If you are stuck anywhere between any coding problem, just visit Queslers to get the Insertion Sort List LeetCode Solution

Find on LeetCode

##### Conclusion:

I hope this Insertion Sort List LeetCode Solution would be useful for you to learn something new from this problem. If it helped you then don’t forget to bookmark our site for more Coding Solutions.

This Problem is intended for audiences of all experiences who are interested in learning about Data Science in a business context; there are no prerequisites.

Keep Learning!

More Coding Solutions >>

LeetCode Solutions

Hacker Rank Solutions

CodeChef Solutions