<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 get_med(head):
            slow=fast=head
            while fast and fast.next and fast.next.next:
                slow=slow.next
                fast=fast.next.next
            return slow
        
            
        
        def merge_sort(head):
            
            if (not head) or (not head.next):
                return head

            head1=head
            med=get_med(head)
            head2=med.next
            med.next=None

            head1=merge_sort(head1)
            head2=merge_sort(head2)

            head=tail=ListNode()
            heads=[head1, head2]

            while heads[0] or heads[1]:
                val1=heads[0].val if heads[0] else 10**10
                val2=heads[1].val if heads[1] else 10**10
                min_idx=val1>val2
                tail.next=heads[min_idx]
                heads[min_idx]=heads[min_idx].next
                tail=tail.next
            
            return head.next

        return merge_sort(head)


        # def getMed(head):
        #     if (head==None) or (head.next==None):
        #         return head
        #     slow,fast=head,head.next
        #     while slow.next and fast.next:
        #         slow=slow.next
        #         fast=fast.next.next
        #     return slow

        # def mergeSort(head):
        #     if (head==None) or (head.next==None):
        #         return head
        #     left,right=head,getMed(head)
        #     tmp=right
        #     right=right.next
        #     tmp.next=None
        #     left,right=mergeSort(left),mergeSort(right)

        #     res=tail=ListNode()
        #     while left and right:
        #         if left.val<=right.val:
        #             tail.next=left
        #             left=left.next
        #         else:
        #             tail.next=right
        #             right=right.next
        #         tail=tail.next
        #     if left:
        #         tail.next=left
        #     if right:
        #         tail.next=right
        #     return res.next
        
        # return mergeSort(head)



        
31600