http://www.geeksforgeeks.org/merge-sort-for-linked-list/

 #include <iostream>
#include <vector>
#include <algorithm>
#include <queue>
#include <stack>
#include <string>
#include <fstream>
#include <map>
#include <set>
using namespace std; struct node {
int data;
node *next;
node() : data(), next(NULL) { }
node(int d) : data(d), next(NULL) { }
}; void push(node* &head, int k) {
node *new_node = new node(k);
new_node->next = head;
head = new_node;
} void print(node* head) {
if (!head) return;
cout << head->data << " ";
print(head->next);
} void frontbacksplit(node *head, node *&a, node *&b) {
node *p, *q;
if (!head || !head->next) {
a = head;
b = NULL;
return;
}
p = head;
q = head->next;
while (q) {
q = q->next;
if (q) {
q = q->next;
p = p->next;
}
}
a = head;
b = p->next;
p->next = NULL;
} node *sortmerge(node *a, node *b) {
node *ans = NULL;
if (!a) return b;
if (!b) return a;
if (a->data < b->data) {
ans = a;
ans->next = sortmerge(a->next, b);
}
else {
ans = b;
ans->next = sortmerge(a, b->next);
}
return ans;
} void mergesort(node *&head) {
if (!head || !head->next) return;
node *a, *b;
node *h = head;
frontbacksplit(h, a, b);
mergesort(a);
mergesort(b);
head = sortmerge(a, b);
} int main() {
node *head = NULL;
push(head, );
push(head, );
push(head, );
push(head, );
push(head, );
push(head, );
mergesort(head);
print(head);
return ;
}

最新文章

  1. centos 6.5 升级内核 linux 3.12.17 (笔记 实测)
  2. Constraint4:default约束
  3. sizeof和strlen()区别
  4. uC/OS-II任务(OS_task)块
  5. Golang 安装及配置教程 for Mac
  6. [奇葩 bug]视图在 ipad5 上正常显示,在 iPad3上超出了边界
  7. 【转】VS2012编译出来的程序,在XP上运行,出现“.exe 不是有效的 win32 应用程序” “not a valid win32 application”
  8. 白书P60 - 硬币问题
  9. httpclient response 重定向
  10. Android:简单的弹幕效果达到
  11. J2EE判断重复的数据
  12. firefox 28.0
  13. Druid连接池配置(java无框架)
  14. 前端工程之CDN部署
  15. New UWP Community Toolkit - Staggered panel
  16. 如何成为java高手
  17. &lt;亲测&gt;CentOS7yum安装PHP7.2
  18. win7开始菜单路径
  19. C#复制数组的两种方式,以及效率比较
  20. 使用pidstat监控资源使用

热门文章

  1. adb pull adb push
  2. 安装npm install时,长时间停留在某一处的解决方案
  3. Qt Creator中增加新的ui文件时报错
  4. laravel处理ajax的post提交
  5. 解读Unity中的CG编写Shader系列3——表面剔除与剪裁模式
  6. android读取xml文件来实现省份,城市,区的选择
  7. iOS 渐变色实现,渐变色圆环,圆环进度条
  8. Spring在注入bean异常时的替换
  9. apache占用内存高解决办法
  10. ftp put get 的使用方法