<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 dq(head, tail):

            if head == tail:
                return    


            curr = head
            largeList = True
            for i in range(1000):
                if curr == tail:
                    largeList = False
                    break
                else:
                    curr = curr.next
            
            curr=head
            if largeList:
                randstep = randrange(0, 500)    
                for i in range(randstep):
                    curr = curr.next
        
                curr.val, tail.val = (tail.val, curr.val)



            # ll=[]
            # pt = head
            # print("Before while, head.val = ", head.val, ", tail.val = ", tail.val)

            # while pt!=None and pt != tail:
            #     ll.append(pt.val)
            #     pt = pt.next
            # ll.append(pt.val)
            # print("  before swap, list = ", ll)

            fast = head
            prev = None
            slow = head

            while fast != tail:
                if fast.val < tail.val:
                    # print("    before swap, slow.val = ", slow.val, "fast.val = ", fast.val )
                    slow.val, fast.val = (fast.val, slow.val)
                    # print("    after swap, slow.val = ", slow.val, "fast.val = ", fast.val )
                    prev = slow
                    slow = slow.next

                fast = fast.next

            if slow != tail:
                slow.val, tail.val = (tail.val, slow.val)

            # ll=[]
            # pt = head
            # while pt!=None and pt != tail:
            #     ll.append(pt.val)
            #     pt = pt.next
            # ll.append(pt.val)
            # print("  after swap, list = ", ll)
 
            # print("Before recursion 1, head.val = ", head.val, ", prev.val = ", prev.val, "slow.val = ", slow.val, "tail.val = ", tail.val)    
            if head != slow:
                dq(head, prev)
            # print("Before recursion 2, head.val = ", head.val, ", prev.val = ", prev.val, "slow.val = ", slow.val, "tail.val = ", tail.val)    
            if slow != tail:
                dq(slow.next, tail) 


        if head == None or head.next == None:
            return head
        
        curr = head
        tail = None
        
        while curr != None:
            tail = curr
            curr = curr.next
        
        dq(head, tail)

        return head
32900