#include <iostream>
#include <vector>
#include <stack>
using namespace std;
struct ListNode {
int val;
ListNode *next;
ListNode () = default;
ListNode (int val): val(val), next(nullptr) {}
ListNode (int val, ListNode *next): val(val), next(next) {}
};
//
ListNode* next_k (ListNode *head, int k) {
while (k --) {
if (head->next == nullptr) break;
head = head -> next;
}
return head;
}
// 找到链表中间节点并切断
ListNode* cut_half (ListNode *head) {
ListNode *slow = head, *fast = head->next;
while (fast != nullptr) {
fast = fast->next;
if (fast) {
fast = fast->next;
// if (fast == nullptr) break; // 奇数时前面少
slow = slow->next;
}
}
head = slow->next;
slow->next = nullptr;
return head;
}
// 合并两个有序链表
ListNode* merge (ListNode *h1, ListNode *h2) {
ListNode *head = new ListNode(), *pre = head;
while (h1 != nullptr && h2 != nullptr) {
if (h1->val <= h2->val) {
pre = pre->next = h1;
h1 = h1->next;
} else {
pre = pre->next = h2;
h2 = h2->next;
}
}
if (h1 != nullptr) pre->next = h1;
if (h2 != nullptr) pre->next = h2;
return head->next;
}
// 递归归并排序
ListNode* merge_sort (ListNode *head) {
if (head == nullptr || head->next == nullptr) return head;
ListNode *h2 = cut_half(head);
head = merge_sort(head);
h2 = merge_sort(h2);
return merge(head, h2);
}
// 非递归归并排序
ListNode* merge_sort_no_recur (ListNode *head, int n) {
ListNode *pre = new ListNode(0, head);
for (int i = 1; i < n; i <<= 1) {
ListNode *now = pre->next, *p = pre;
for (int j = 1; j+i <= n; j += i*2) {
ListNode *h2 = next_k(now, i-1), *tmp = h2->next;
h2->next = nullptr; h2 = tmp; // 前半部分尾部切断
ListNode *last = next_k(h2, i-1), *nt = last->next;
last->next = nullptr;
p->next = merge(now, h2);
while (last->next != nullptr) last = last->next;
now = nt; p = last;
}
p->next = now;
}
return pre->next;
}
void print (ListNode *head) {
while (head != nullptr) {
cout << head->val << " ";
head = head->next;
}
cout << endl;
}
ListNode* MergeList(ListNode* p1, ListNode *p2) {
ListNode* head = p1->val < p2->val ? p1 : p2, *now = head;
if (head == p1) p1 = p1->next;
else p2 = p2->next;
while (p1 != nullptr && p2 != nullptr) {
if (p1->val < p2->val) {
now->next = p1;
p1 = p1->next;
} else {
now->next = p2;
p2 = p2->next;
}
now = now->next;
}
if (p1 != nullptr) {
now->next = p1;
} else {
now->next = p2;
}
return head;
}
ListNode* MergeSort2(ListNode *head) {
std::queue<ListNode*> nodes;
while (head != nullptr) {
ListNode* nxt = head->next;
head->next = nullptr;
nodes.push(head);
head = nxt;
}
while (nodes.size() > 1) {
ListNode* p1 = nodes.front(); nodes.pop();
ListNode* p2 = nodes.front(); nodes.pop();
nodes.push(MergeList(p1, p2));
}
return nodes.front();
}
int main()
{
ListNode *head = new ListNode, *pre = head, *now = nullptr;
vector<int> a = {2,1,5,9,6,7,4,3,8,0};
int n = a.size();
for (int i = 0; i < n; ++ i) {
now = new ListNode(a[i]);
pre = pre -> next = now;
}
head = head->next;
// head = merge_sort (head);
head = merge_sort_no_recur(head, n);
print(head);
return 0;
}