<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>


class Solution {
public:
    // Merge two sorted linked lists
    ListNode* merge(ListNode* l1, ListNode* l2) {
        ListNode* dummy = new ListNode();
        ListNode* temp = dummy;
        
        // Merge two lists
        while (l1 != nullptr && l2 != nullptr) {
            if (l1->val < l2->val) {
                temp->next = l1;
                l1 = l1->next;
            } else {
                temp->next = l2;
                l2 = l2->next;
            }
            temp = temp->next;
        }

        // If any elements remain in either list
        if (l1 != nullptr) temp->next = l1;
        else temp->next = l2;

        return dummy->next;
    }

    // Split the list into two lists of size `size`, return the next sublist to process
    ListNode* split(ListNode* head, int size) {
        for (int i = 1; head != nullptr && i < size; i++) {
            head = head->next;
        }
        if (head == nullptr) return nullptr;

        ListNode* second = head->next;
        head->next = nullptr;  // Break the list

        return second;  // Return the starting point of the next sublist
    }

    // Main iterative merge sort function
    ListNode* sortList(ListNode* head) {
        if (!head || !head->next) return head;  // Base case: empty or single-node list
        
        // Step 1: Get the length of the list
        ListNode* curr = head;
        int length = 0;
        while (curr != nullptr) {
            length++;
            curr = curr->next;
        }

        ListNode* dummy = new ListNode(0);
        dummy->next = head;
        ListNode* left;
        ListNode* right;
        ListNode* tail;

        // Step 2: Iteratively merge sublists
        for (int size = 1; size < length; size *= 2) {
            curr = dummy->next;
            tail = dummy;

            while (curr != nullptr) {
                left = curr;
                right = split(left, size);  // Split the list into two halves
                curr = split(right, size);  // Get the next sublist

                // Merge two halves and attach to tail
                tail->next = merge(left, right);

                // Move tail to the end of the merged list
                while (tail->next != nullptr) {
                    tail = tail->next;
                }
            }
        }

        return dummy->next;
    }
};

61608