<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]:
        if not head or not head.next:
            return head
        def mid(head):
            slow,fast = head,head.next
            while fast and fast.next:
                slow = slow.next
                fast = fast.next.next
            return slow
        def merge(l1,l2):
            dummy = ListNode()
            tail = dummy
            while l1 and l2:
                if l1.val<l2.val:
                    tail.next = l1
                    l1 = l1.next
                else:
                    tail.next = l2
                    l2 = l2.next
                tail = tail.next
            if l1:
                tail.next = l1
            if l2:
                tail.next = l2
            return dummy.next
        left = head
        right = mid(head)
        tmp = right.next
        right.next = None
        right = tmp
        left = self.sortList(left)
        right = self.sortList(right)
        return merge(left,right)
            















        if not head or not head.next:
            return head
        def mid(head):
            slow,fast = head,head.next
            while fast and fast.next:
                slow = slow.next
                fast = fast.next.next
            return slow
        def merge(l1,l2):
            dummy = ListNode()
            tail = dummy
            while l1 and l2:
                if l1.val<l2.val:
                    tail.next = l1
                    l1 = l1.next
                else:
                    tail.next = l2
                    l2 = l2.next
                tail = tail.next
            if l1:
                tail.next = l1
            if l2:
                tail.next = l2
            return dummy.next
        left = head
        right = mid(head)
        tmp = right.next
        right.next = None
        right = tmp
        left = self.sortList(left)
        right = self.sortList(right)
        return merge(left,right)












        if not head or not head.next:
            return head
        def mid(head):
            slow,fast = head,head.next
            while fast and fast.next:
                slow = slow.next
                fast = fast.next.next
            return slow
        def merge(l1,l2):
            dummy = ListNode()
            tail = dummy
            while l1 and l2:
                if l1.val<l2.val:
                    tail.next = l1
                    l1 = l1.next
                else:
                    tail.next = l2
                    l2 = l2.next
                tail = tail.next
            if l1:
                tail.next = l1
            if l2:
                tail.next = l2
            return dummy.next
        left = head
        right = mid(head)
        tmp = right.next
        right.next = None
        right = tmp

        left = self.sortList(left)
        right = self.sortList(right)
        return merge(left,right)
        











        if not head or not head.next:
            return head
        def merge(l1,l2):
            dummy = ListNode()
            tail = dummy
            while l1 and l2:
                if l1.val<l2.val:
                    tail.next = l1
                    l1 = l1.next
                else:
                    tail.next = l2
                    l2 = l2.next
                tail = tail.next
            if l1:
                tail.next = l1
            if l2:
                tail.next = l2
            return dummy.next

        def mid(head):
            slow,fast = head,head.next
            while fast and fast.next:
                fast = fast.next.next
                slow= slow.next
            return slow
        left = head
        right = mid(head)
        tmp = right.next
        right.next = None
        right = tmp
        left = self.sortList(left)
        right = self.sortList(right)
        return merge(left,right)















        if not head or not head.next:
            return head
        def mid(head):
            slow,fast = head,head.next
            while fast and fast.next:
                slow = slow.next
                fast = fast.next.next
            return slow
        def merger(l1,l2):
            dummy = ListNode()
            tail = dummy
            while l1 and l2:
                if l1.val>l2.val:
                    tail.next = l2
                    l2 = l2.next
                else:
                    tail.next = l1
                    l1 = l1.next
                tail = tail.next
            if l1:
                tail.next = l1
            if l2:
                tail.next = l2
            return dummy.next
        
        left = head
        right = mid(head)
        tmp = right.next
        right.next = None
        right = tmp
        left = self.sortList(left)
        right = self.sortList(right)
        return merger(left,right)












        if not head or not head.next:return head

        def getmid(head):
            slow,fast = head,head.next
            while fast and fast.next:
                slow = slow.next
                fast = fast.next.next
            return slow
        
        def merge(l1,l2):
            dummy = ListNode(0)
            tail = dummy
            while l1 and l2:
                if l1.val >l2.val:
                    tail.next = l2
                    l2 = l2.next
                else:
                    tail.next = l1
                    l1 = l1.next
                tail = tail.next
            if l1:
                tail.next = l1
            if l2:
                tail.next = l2
            return dummy.next  


        left = head
        right = getmid(head)
        tmp = right.next
        right.next = None
        right = tmp

        left = self.sortList(left)
        right = self.sortList(right)
        return merge(left,right)



















        if not head or not head.next:return head
        def getmid(head):
            slow,fast = head,head.next
            while fast and fast.next:
                slow = slow.next
                fast = fast.next.next
            return slow
        def merge(l1,l2):
            dummy = ListNode()
            tail = dummy
            while l1 and l2:
                if l2.val>l1.val:
                    tail.next = l1
                    l1 = l1.next
                else:
                    tail.next = l2
                    l2 = l2.next
                tail = tail.next
            if l1:
                tail.next = l1
            if l2:
                tail.next = l2
            return dummy.next
        left = head
        right = getmid(head)
        tmp = right.next
        right.next = None
        right = tmp
        left = self.sortList(left)
        right = self.sortList(right)
        return merge(left,right)



















        if not head or not head.next:return head
        
        def getmid(head):
            slow,fast = head,head.next
            while fast and fast.next:
                fast = fast.next.next
                slow = slow.next
            return slow
        
        def merge(l1,l2):
            dummy = ListNode()
            tail = dummy
            while l1 and l2:
                if l1.val<l2.val:
                    tail.next = l1
                    l1 = l1.next
                else:
                    tail.next = l2
                    l2 = l2.next
                tail = tail.next
            if l1:
                tail.next = l1
            if l2:
                tail.next = l2
            return dummy.next 

        left = head
        right = getmid(head)
        tmp = right.next
        right.next = None
        right = tmp
        left = self.sortList(left)
        right = self.sortList(right)
        return merge(left,right)

















        if not head or not head.next:return head
        def getmid(head):
            slow,fast = head,head.next
            while fast and fast.next:
                fast = fast.next.next
                slow = slow.next
            return slow
        
        def merge(l1,l2):
            dummy = ListNode()
            tail = dummy
            while l1 and l2:
                if l1.val<l2.val:
                    tail.next = l1
                    l1 = l1.next
                else:
                    tail.next = l2
                    l2 = l2.next
                tail = tail.next
            if l1:
                tail.next = l1
                
            if l2:
                tail.next = l2
            return dummy.next
                
                
        

        
        left = head
        right = getmid(head)
        tmp = right.next
        right.next = None
        right = tmp
        left = self.sortList(left)
        right = self.sortList(right)
        return merge(left,right)
        


















        if not head or not head.next:return head
        def getmid(head):
            slow,fast = head,head.next
            while fast and fast.next:
                slow = slow.next
                fast = fast.next.next
            return slow
        def sort(l1,l2):
            dummy = ListNode()
            tail = dummy
            while l1 and l2:
                if l1.val>l2.val:
                    tail.next = l2
                    l2 = l2.next
                else:
                    tail.next = l1
                    l1 = l1.next
                tail = tail.next
            if l1:
                tail.next = l1
            if l2:
                tail.next = l2
            return dummy.next
        left = head
        right = getmid(head)
        tmp = right.next
        right.next = None
        right = tmp

        left = self.sortList(left)
        right = self.sortList(right)
        return sort(left,right)























        if not head or not head.next:return head

        def getMid(head):
            slow,fast = head,head.next
            while fast and fast.next:
                slow = slow.next
                fast = fast.next.next
            return slow
        def merge(l1,l2):
            dummy = ListNode()
            tail = dummy
            while l1 and l2:
                if l1.val<l2.val:
                    tail.next = l1
                    l1 = l1.next
                else:
                    tail.next = l2
                    l2 = l2.next
                tail = tail.next
            if l1:
                tail.next = l1
            if l2 : 
                tail.next = l2
            return dummy.next


        left = head
        right = getMid(head)
        tmp = right.next
        right.next = None
        right = tmp

        left  = self.sortList(left)
        right = self.sortList(right)

        return merge(left,right)


























        
        if not head or not head.next:return head
        def getMid(head):
            slow,fast = head,head.next
            while fast and fast.next:
                slow = slow.next
                fast = fast.next.next
            return slow
        def merge(l1,l2):
            dummy = ListNode()
            tail = dummy
            while l1 and l2:
                if l1.val <l2.val:
                    tail.next = l1
                    l1 = l1.next
                else:
                    tail.next = l2
                    l2 = l2.next
                tail = tail.next
            if l1:
                tail.next = l1
                tail = tail.next
            if l2:
                tail.next = l2
                tail = tail.next
            return dummy.next

        

        left = head
        right = getMid(head)
        tmp = right.next
        right.next = None
        right = tmp

        left = self.sortList(left)
        right = self.sortList(right)

        return merge(left,right)

        
                






























        if not head or not head.next:return head

        left = head
        right = self.getMid(head)
        tmp = right.next
        right.next = None
        right = tmp

        left = self.sortList(left)
        right = self.sortList(right)
        return self.merge(left,right)
    
    def getMid(self,head):
        slow,fast = head,head.next
        while fast and fast.next:
            slow = slow.next
            fast = fast.next.next
        return slow
    
    def merge(self,list1,list2):
        dummy = ListNode()
        tail = dummy
        while list1 and list2:
            if list1.val<list2.val:
                tail.next = list1
                list1 = list1.next
            else:
                tail.next = list2
                list2 = list2.next
        if list1:
            tail.next = list1
        if list2:
            tail.next = list2
        return dummy.next
    


































        if not head or not head.next:return head
        def getMid(head):
            slow,fast = head,head.next
            while fast and fast.next:
                slow = slow.next
                fast = fast.next.next
            return slow
        def merge(l1,l2):
            dummy = ListNode()
            tail = dummy
            while l1 and l2:
                if l1.val<l2.val:
                    tail.next = l1
                    l1 = l1.next
                else:
                    tail.next = l2
                    l2 = l2.next
                tail = tail.next
            if l1:
                tail.next = l1
                tail = tail.next
            if l2:
                tail.next = l2
                tail = tail.next
            return dummy.next
        left = head
        right = getMid(head)
        tmp = right.next
        right.next = None
        right = tmp

        left = self.sortList(left)
        right = self.sortList(right)
        return merge(left,right)









































        if not head or not head.next:
            return head
        
        left = head
        right = self.getmid(head)
        tmp = right.next
        right.next = None
        right = tmp

        left = self.sortList(left)
        right = self.sortList(right)
        return self.merge(left,right)

    def getmid(self,head):
        slow,fast = head,head.next
        while fast and fast.next:
            slow = slow.next
            fast = fast.next.next
        return slow
    
    def merge(self,l1,l2):
        dummy = ListNode()
        tail = dummy
        while l1 and l2:
            if l1.val<l2.val:
                tail.next = l1
                l1 = l1.next
            else:
                tail.next = l2
                l2 = l2.next
            tail = tail.next
        if l1:
            tail.next = l1
        if l2:
            tail.next = l2
        return dummy.next












































        if not head:
            return None
        myList = []
        while head:
            myList.append(head.val)
            head = head.next
        myList.sort()
        newHead = point = ListNode(0)
        for i in range(len(myList)):
            point.next = ListNode(myList[i])
            point = point.next
        return newHead.next
       
    
    
    
    
    """
        if not head or not  head.next:
            return head
        mid = self.midpoint(head)
        left = self.sortList(head)
        right = self.sortList(mid)
        return self.merge(left,right)
    
    def midpoint(self,head):
        slow,fast = head,head
        while fast.next and fast.next.next:
            slow = slow.next
            fast = fast.next.next
        mid = slow.next
        slow.next = None
        return mid
    
    def merge(self , left ,right):
        dummy = ListNode()
        cur = dummy
        while left and right:
            if left.val<right.val:
                cur.next = left
                left = left.next
                cur  = cur.next
            else:
                cur.next = right
                right = right.next
                cur = cur.next
        cur.next = left or right
        return dummy.next"""
            
        
33300