<p>Given the <code>head</code> of a linked list, return <em>the list after sorting it in <strong>ascending order</strong></em>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/09/14/sort_list_1.jpg" style="width: 450px; height: 194px;" />
<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/2020/09/14/sort_list_2.jpg" style="width: 550px; height: 184px;" />
<pre>
<strong>Input:</strong> head = [-1,5,3,4,0]
<strong>Output:</strong> [-1,0,3,4,5]
</pre>

<p><strong class="example">Example 3:</strong></p>

<pre>
<strong>Input:</strong> head = []
<strong>Output:</strong> []
</pre>

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

<ul>
	<li>The number of nodes in the list is in the range <code>[0, 5 * 10<sup>4</sup>]</code>.</li>
	<li><code>-10<sup>5</sup> &lt;= Node.val &lt;= 10<sup>5</sup></code></li>
</ul>

<p>&nbsp;</p>
<p><strong>Follow up:</strong> Can you sort the linked list in <code>O(n logn)</code> time and <code>O(1)</code> memory (i.e. constant space)?</p>

# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, val=0, next=None):
#         self.val = val
#         self.next = next
class Solution:
    def sortList(self, head: Optional[ListNode]) -> Optional[ListNode]:
        n = 0
        cur_v = head
        while cur_v:
            n += 1
            cur_v = cur_v.next
        if n == 0:
            return head

        def merge(pv, v, i, k, j):
            left_head, right_head = v, v
            for _ in range(i, k):
                right_head = right_head.next
            new_head = None

            took_left, took_right = 0, 0
            full_left, full_right = k - i, j - k
            cur_v = pv
            for _ in range(i, j):
                takel, taker = False, False
                # print(left_head, right_head, full_left, full_right)
                if took_left < full_left and took_right < full_right:
                    if left_head.val < right_head.val:
                        takel = True
                    else:
                        taker = True
                # print(takel, taker)
                if takel or (not taker and took_left < full_left):
                    took_left += 1
                    if cur_v is not None:
                        cur_v.next = left_head
                        cur_v = cur_v.next
                    else:
                        cur_v = left_head
                        new_head = cur_v
                    left_head = left_head.next
                else:
                    took_right += 1
                    if cur_v is not None:
                        cur_v.next = right_head
                        cur_v = cur_v.next
                    else:
                        cur_v = right_head
                        new_head = cur_v
                    right_head = right_head.next
            cur_v.next = right_head
            # print(cur_v, cur_v.next, new_head)
            return cur_v, right_head, new_head
        
        w = 1
        while w < n:
            i = 0
            cur_v = head
            last_v = None
            while i < n - 1:
                j = min(n, i + 2 * w)
                k = min(n, i + w)
                if j != k:
                    p_v, cur_v, new_head = merge(last_v, cur_v, i, k, j)
                    # if last_v is not None:
                    #     last_v.next = new_head
                    # else:
                    if i == 0:
                        head = new_head
                    last_v = p_v
                i += 2 * w
            w *= 2
        return head
31800