<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) {}
 * };
 */
 #define MAX_SIZE 50000
class Solution {
public:
    /*void printList(ListNode* head, int max){
        cout<<"[";
        int i=0;
        while(head &&i++<max)
        {
            cout<<head->val<<",";
            head=head->next;
        }
        cout<<"]"<<endl;
    }*/
    void printList(ListNode* head){
        cout<<"[";
        while(head)
        {
            cout<<head->val<<",";
            head=head->next;
        }
        cout<<"]"<<endl;
    }
    /*void qsortList(ListNode* head,int len)
    {
        //printList(head,maxLen);
        if(len==2)
        {
            if(head->next->val<head->val)
            {
                swap(head->next->val,head->val);
            }
            return;
        }
        ListNode* pivot=head;
        int i;
        for(i=0;i<len/2;i++)
        {
            pivot=pivot->next;
        }
        int pv=pivot->val;
        swap(pivot->val,head->val);
        //cout<<"pv="<<pv<<" pivot->val="<<pivot->val<<" head->val="<<head->val<<endl;
        pivot=head;
        ListNode* cur=head;
        ListNode* prev=nullptr;
        i=-1;
        for(int j=0;j<len;j++)
        {
            if(cur->val<=pv)
            {
                prev=pivot;
                i++;
                if(i!=j)
                {
                    swap(pivot->val,cur->val);
                }
                pivot=pivot->next;
            }
            cur=cur->next;
        }
        //cout<<"i="<<i<<" pivot->val="<<pivot->val<<endl;
        if(prev)
        {
            //cout<<"prev-val="<<prev->val<<endl;
            swap(prev->val,head->val);
        }
        //printList(head);
        if(i>1)
        {
            qsortList(head,i);
        }
        if(len-i>2)
        {
            qsortList(pivot,len-i-1);
        }
                
    }*/
    ListNode* sortList(ListNode* head) {
        if(!head || !head->next)
        {
            return head;
        }
        int len=0;
        vector<int> nodes(MAX_SIZE);
        ListNode* cur=head;
        while(cur)
        {
            nodes[len]=cur->val;
            cur=cur->next;
            len++;
        }
        sort(nodes.begin(),nodes.begin()+len);
        cur=head;
        for(int i=0;i<len;i++)
        {
            cur->val=nodes[i];
            cur=cur->next;
        }
        //cout<<"len="<<len<<endl;
        //qsortList(head,len);
        return head;
    }
};
58781