<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.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode() : val(0), next(nullptr) {}
 *     ListNode(int x) : val(x), next(nullptr) {}
 *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };
 */
class Solution {
    int getLength(ListNode* head) {
        int l = 0;
        while (head) {
            ++l;
            head = head->next;
        }
        return l;
    }

    pair<ListNode*, ListNode*> quicksort(ListNode* head, int len) {
        if (!head || !head->next) return {head, head};

        ListNode* pivot;
        int pnum = rand() % len;
        if (!pnum) {
            pivot = head;
            head = head->next;
        } else {
            auto cur = head;
            while(--pnum)
                cur = cur->next;
            pivot = cur->next;
            cur->next = pivot->next;
        }
        pivot->next = NULL;

        ListNode *ll = NULL, *rl = NULL;
        int llen = 0, rlen = 0;
        while (head) {
            ListNode* nxt = head->next;
            if (head->val < pivot->val) {
                head->next = ll;
                ll = head;
                ++llen;
            } else {
                head->next = rl;
                rl = head;
                ++rlen;
            }

            head = nxt;
        }

        auto [lh, lt] = quicksort(ll, llen);
        auto [rh, rt] = quicksort(rl, rlen);

        ListNode *mh = lh, *mt = rt;
        if (!lt)
            mh = pivot;
        else
            lt->next = pivot;

        pivot->next = rh;
        if (!rt)
            mt = pivot;

        return {mh, mt};
    }

public:
    ListNode* sortList(ListNode* head) {
        return quicksort(head, getLength(head)).first;
    }
};

auto _ = [](){ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); return 0;}();
19196