<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 *nextHead;
ListNode *prevEnd;
    ListNode *breakList(ListNode *head, int n) {
        ListNode *mid = head, *midPrev = head, *fast = head->next ? head->next : head;
        int i;
        for (i = 1; i < n && (midPrev->next || fast->next); i++) {
            //if (midPrev->next)
                midPrev = midPrev->next;
            //else break;
            if (fast && fast->next)
                fast = fast->next;
            //else break;
            if (fast->next) fast = fast->next;
            //else break;
        }
        mid = midPrev->next;
        midPrev->next = NULL;
        nextHead = fast ? fast->next : NULL;
        if (fast) fast->next = NULL;
        return mid;
    }
    ListNode *mergeList(ListNode *l1, ListNode *l2) {
        ListNode *dummy = new ListNode (-1);
        ListNode *cur = dummy;
        while (l1 && l2) {
            if (l1->val < l2->val) {
                cur->next = l1;
                l1 = l1->next;
            } else {
                cur->next = l2;
                l2 = l2->next;
            }
            cur = cur->next;
            prevEnd = cur;
        }
        if (l1) cur->next = l1;
        if (l2) cur->next = l2;
        while (cur) {
            prevEnd = cur;
            cur = cur->next;
        }
        return dummy->next;
    }
    
    ListNode* sortList(ListNode* head) {
        ListNode *dummy = new ListNode(-1, head);
        ListNode *cur = dummy->next;
        int n = 0;
        while (cur) {
            cur = cur->next;
            n++;
        }
        cur = dummy;
        prevEnd = dummy;
        int len = 1;
        ListNode *l1Head = head, *l2Head = head;
        while (len < n) {
            // cout << "len: " << len << "\n";
            l1Head = dummy->next;
            cur = dummy;
            while (l1Head) {
                l2Head = breakList(l1Head, len);
                // cout << "l1: " << l1Head->val << " ";
                // if (l2Head)
                //     cout << "l2: " << l2Head->val << "\n";
                cur->next = mergeList(l1Head, l2Head);
                l1Head = nextHead;
                // if (nextHead)
                //     cout << "nextHead: " << nextHead->val << "\n";
                cur = prevEnd;
            }
            // print(dummy->next);
            len *= 2;
        }
        return dummy->next;
    }
};
62551