<p>Given the <code>head</code> of a singly linked list, sort the list using <strong>insertion sort</strong>, and return <em>the sorted list&#39;s head</em>.</p>

<p>The steps of the <strong>insertion sort</strong> algorithm:</p>

<ol>
	<li>Insertion sort iterates, consuming one input element each repetition and growing a sorted output list.</li>
	<li>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.</li>
	<li>It repeats until no input elements remain.</li>
</ol>

<p>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.</p>
<img alt="" src="https://upload.wikimedia.org/wikipedia/commons/0/0f/Insertion-sort-example-300px.gif" style="height:180px; width:300px" />
<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/04/sort1linked-list.jpg" style="width: 422px; height: 222px;" />
<pre>
<strong>Input:</strong> head = [4,2,1,3]
<strong>Output:</strong> [1,2,3,4]
</pre>

<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/04/sort2linked-list.jpg" style="width: 542px; height: 222px;" />
<pre>
<strong>Input:</strong> head = [-1,5,3,4,0]
<strong>Output:</strong> [-1,0,3,4,5]
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li>The number of nodes in the list is in the range <code>[1, 5000]</code>.</li>
	<li><code>-5000 &lt;= Node.val &lt;= 5000</code></li>
</ul>

# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, val=0, next=None):
#         self.val = val
#         self.next = next
class Solution:
    def insertionSortList(self, head: Optional[ListNode]) -> Optional[ListNode]:
        if not head or not head.next:
            return head

        dummy = ListNode(next=head)
        prev = head
        cur = head.next

        while cur:
            if cur.val >= prev.val:
                prev, cur = cur, cur.next
                continue

            cur_sorted = dummy
            while cur_sorted.next and cur_sorted.next.val < cur.val:
                cur_sorted = cur_sorted.next

            prev.next = cur.next # no need to move prev as we move cur so prev can stay the same
            cur.next, cur_sorted.next, cur = cur_sorted.next, cur, cur.next

        return dummy.next

# Time: worst case: O(n ^ 2) but best case O(n)
# Space: O(1)


    def insertionSortList2(self, head: Optional[ListNode]) -> Optional[ListNode]:
        if not head or not head.next:
            return head

        dummy = ListNode()
        curr = head

        while curr:
            prev = dummy
            while prev.next and prev.next.val < curr.val:
                prev = prev.next

            curr.next, prev.next, curr = prev.next, curr, curr.next

        return dummy.next

# Time: O(n ^ 2)
# Space: O(1)


    def insertionSortList2(self, head: Optional[ListNode]) -> Optional[ListNode]:
        if not head or not head.next:
            return head

        sorted_head = ListNode(next=head)
        cur = head.next
        head.next = None  # cut the link!

        while cur:
            cur_sorted = sorted_head
            while cur_sorted.next and cur_sorted.next.val < cur.val:
                cur_sorted = cur_sorted.next

            cur.next, cur_sorted.next, cur = cur_sorted.next, cur, cur.next

        return sorted_head.next

# Time: O(n ^ 2)
# Space: O(1)


    # not working as no clear delimitation between sorted and non sorted part
    def insertionSortList_not(self, head: Optional[ListNode]) -> Optional[ListNode]:
        if not head or not head.next:
            return head

        sorted_head = ListNode(next=head)
        cur = head.next

        while cur:
            cur_sorted = sorted_head
            while cur_sorted.next and cur_sorted.next.val < cur.val:
                cur_sorted = cur_sorted.next

            cur.next, cur_sorted.next, cur = cur_sorted.next, cur, cur.next

        return sorted_head.next

# Time: O(n ^ 2)
# Space: O(1)
18300