<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) {}
 * };
 */

typedef pair<ListNode*,ListNode*> LinkedList;

class Solution {
public:
    ListNode* sortList(ListNode* head) {
        if(head == nullptr)
            return nullptr;
        
        int sort_len = 1;
        LinkedList sorted_part = sort_first_k(head, sort_len);
        while(sorted_part.second) {
            LinkedList the_other_sorted_part = sort_first_k(sorted_part.second, sort_len);
            ListNode* merge_head = merge(sorted_part.first, the_other_sorted_part.first);
            sorted_part = LinkedList( merge_head, the_other_sorted_part.second );
            sort_len += 1;
        }
        return sorted_part.first;
    }

    LinkedList sort_first_k(ListNode* head, int k) {
        
        // the list "head" is guaranteed to be non-empty
        if(k == 1) {
            LinkedList ans{head, head->next};
            head->next = nullptr;   // make sure a list is ended with nullptr
            return ans;
        }

        LinkedList left_part = sort_first_k(head, (k +1) / 2);
        if(left_part.second == nullptr)
            return left_part;
        else {
            LinkedList right_part = sort_first_k(left_part.second, k - (k+1)/2);
            return LinkedList( merge(left_part.first, right_part.first), right_part.second );
        }
    }

    ListNode* merge(ListNode* ptr_1, ListNode* ptr_2) {
        // the end of list is indicated by nullptr
        ListNode* prehead = new ListNode();
        ListNode* ptr = prehead;

        while(ptr_1 != nullptr && ptr_2 != nullptr) {
            // ensure stability
            if(ptr_1->val <= ptr_2->val) {
                ptr->next = ptr_1;
                ptr = ptr->next;
                ptr_1 = ptr_1->next;
            } else {
                ptr->next = ptr_2;
                ptr = ptr->next;
                ptr_2 = ptr_2->next;
            }
        }

        if(ptr_1 == nullptr) {
            ptr->next = ptr_2;
        } else {
            ptr->next = ptr_1;
        }

        return prehead->next;
    }
};
64436