<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]:
        def _split(head): 
            # print("_split=", head if head else None)
            if not head:
                return None, None
            head1, head2 = head, head
            ptr_slow, ptr_fast = head1, head1.next
            if not ptr_fast:
                return head, None
            if not ptr_fast.next:
                head.next = None
                return head, ptr_fast
            ptr_fast = ptr_fast.next
            while ptr_fast and ptr_fast.next:
                ptr_fast = ptr_fast.next.next
                ptr_slow = ptr_slow.next
            head2 = ptr_slow.next
            ptr_slow.next = None # terminate  head1
            # print("split head1 = ", head1 if head1 else None)
            # print("split head2 = ", head2 if head2 else None)
            return head1, head2
        def _merge(head1, head2):
            if head1.val < head2.val:
                head = head1
                head1 = head1.next                
            else:
                head = head2
                head2 = head2.next
            ptr = head
            while head1 and head2:
                if head1.val < head2.val:
                    ptr.next = head1
                    head1 = head1.next                
                else:
                    ptr.next = head2
                    head2 = head2.next
                ptr = ptr.next
            if head1:
                ptr.next = head1
            if head2:
                ptr.next = head2
            # print("merged", head)
            return head
        head1, head2 = _split(head)
        if not head1:
            return head2
        if not head2:
            return head1
        return _merge(self.sortList(head1), self.sortList(head2))

31900