<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* sortList(ListNode* head) {
        return mergeSort(head);
    }
    ListNode* mergeSort(ListNode*& head){
        if (head == nullptr || head->next == nullptr) return head; // Caso base, final de la lista

        ListNode* middle = splitList(head).second; //Divido la lista por el nodo de la mitad que sera middle
        ListNode* left = head; //Nodo a la izquierda de middle, que ordenara la parte anterior a este
        ListNode* right = middle; //Nodo que ordenara la parte depúes de middle

        left = mergeSort(left);   //Vuelvo a dividir las listas recurisvamente
        right = mergeSort(right); 

        return merge(left, right); 
    }
    ListNode* merge(ListNode* left,ListNode* right){
        ListNode dummy(0);
        ListNode* tail = &dummy; //Creo una lista donde insertare los nodos ordenados
        while(left != nullptr && right != nullptr){ //Realizo la ordenacion hasta que se acaba una lista
            if(left->val < right->val){
                tail->next = left;
                left = left->next;
            }else{
                tail->next = right;
                right = right->next;
            }
            tail = tail->next;
        }

        tail->next = (left != nullptr) ? left : right;


        return dummy.next;
    }

    std::pair<ListNode*, ListNode*> splitList(ListNode* head) {
        int length = countNodes(head); // Contar los nodos de la lista
        if (length == 0) return {nullptr, nullptr};
        
        int mid = length / 2;
        ListNode* current = head;
        ListNode* prev = nullptr;
        for (int i = 0; i < mid; i++) {
            prev = current;
            current = current->next;
        }
        
        if (prev != nullptr) {
            prev->next = nullptr; // Dividir la lista
        }
        
        return {head, current}; // Retorna las dos mitades
    }
    int countNodes(ListNode* head) {
        int count = 0;
        ListNode* current = head;
        while (current != nullptr) {
            count++;
            current = current->next;
        }
        return count;
    }
};
13541