<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 {
public:

    ListNode* findMid(ListNode* head){
        ListNode* slow = head, *fast = head;
        while(fast && fast->next){
            slow = slow->next;
            fast = fast->next->next;
        }   
        return slow;
    }

    ListNode* mergeTwoLists(ListNode* l1, ListNode* l2){
        if(l1==NULL && l2==NULL)
            return NULL;
        ListNode *temp1 = l1, *temp2 = l2;
        ListNode *dummy = new ListNode(-1);
        ListNode *temp = dummy;
        while(temp1 && temp2){
            if(temp1->val < temp2->val){
                temp->next = temp1;
                temp = temp1;
                temp1 = temp1->next;
            }
            else{
                temp->next = temp2;
                temp = temp2;
                temp2 = temp2->next;
            }
        }
        if(temp1){
            temp->next = temp1;
        }
        if(temp2){
            temp->next = temp2;
        }
        return dummy->next;
    }

    ListNode* sortList(ListNode* head) {
        if(head==NULL || head->next==NULL)
            return head;
        if(head->next->next==NULL){
            if(head->val < head->next->val)
                return head;
            else{
                ListNode* front = head->next;
                front->next = head;
                head->next = NULL;
                return front;
            }
        }
        ListNode* mid = findMid(head);
        ListNode* right = mid->next;
        mid->next = NULL;
        ListNode* left = sortList(head);
        right = sortList(right);
        return mergeTwoLists(left, right);
    }
};
70091