Data Structure Linked List: Merge Sort for Linked Lists
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 ;
}
Data Structure Linked List: Merge Sort for Linked Lists的更多相关文章
- [Linked List]Insertion Sort List
Total Accepted: 59422 Total Submissions: 213019 Difficulty: Medium Sort a linked list using insertio ...
- LeetCode -- Merge Two Sorted Linked List
Question: Merge two sorted linked lists and return it as a new list. The new list should be made by ...
- [Algorithm] Heap data structure and heap sort algorithm
Source, git Heap is a data structure that can fundamentally change the performance of fairly common ...
- 【推荐】Data Structure Visualizations
University of San Francisco David Galles 功能:可视化数据结构&算法实现过程 网站地址 https://www.cs.usfca.edu/~ga ...
- [算法]——归并排序(Merge Sort)
归并排序(Merge Sort)与快速排序思想类似:将待排序数据分成两部分,继续将两个子部分进行递归的归并排序:然后将已经有序的两个子部分进行合并,最终完成排序.其时间复杂度与快速排序均为O(nlog ...
- [转]Data Structure Recovery using PIN and PyGraphviz
Source:http://v0ids3curity.blogspot.com/2015/04/data-structure-recovery-using-pin-and.html --------- ...
- 面试总结之数据结构(Data Structure)
常用数据结构及复杂度 http://www.cnblogs.com/gaochundong/p/3813252.html 常用数据结构的时间复杂度 Data Structure Add Find De ...
- 归并排序(merge sort)
M erge sort is based on the divide-and-conquer paradigm. Its worst-case running time has a lower ord ...
- sicily 1154. Easy sort (tree sort& merge sort)
Description You know sorting is very important. And this easy problem is: Given you an array with N ...
随机推荐
- UINavigationbar/UINavigationItem/UITabBar/UITabButton/UITabBarItem粑粑粑粑~
看着标题是不是乱的一塌糊涂...... . 在开发中,你非常可能就理不清这些关系,刚好闲的蛋疼,来整理一下吧. 一.UINavigationBar.UINavigationItem.UIBarButt ...
- java 实现新浪微博内容计数器 Java问题通用解决代码
http://www.mr3g.net/?p=220 参考sina的js版本而来,费弄最多的时间就是java对ansii码的判断了,js直接就是isascii()函数就可以实现了,java还要想办法 ...
- Repeater绑定List泛型对象
后台: public void BindData() { List<WeiBo> DataList = new List<WeiBo>(); ...
- 不需要Root即可Hook别人APP的方法
*:first-child { margin-top: 0 !important; } body > *:last-child { margin-bottom: 0 !important; } ...
- iOS中三种方式实现登录界面播放视频或gif效果
现在app都做的越来越炫酷,各种动画效果,各种特效很好的提高了用户的体验.很多app在登录界面都使用了动画效果,比如Uber,Keep,QQ等等.这些动画效果基本都是使用gif或者MP4来实现的. 效 ...
- emacs的常用配置备份
据说有人搞丢了自己的emacs的配置,然后一怒之下抛弃了emacs投身vim,我还是做个emacs配置的备份吧, 虽然我现在也算不上emacs的发烧友. 这里的配置大多是从网上参考的,最多的是下面的链 ...
- Linux Apache安装加载mod_deflate模块
为了开启apache服务器中的gzip压缩功能,mod_deflate模块是必须安装加载的.现在介绍如何安装.1.进入到mod_deflate.c目录 cd /lamp/httpd-2.2.20/mo ...
- mysql 归档方案(一次性)
一. 归档流程: 1. 导出需要的数据 2. 创建临时表table_tmp 3. 导入数据到临时表 4. 修改原始表名为table_bak 5. 修改临时表为原始表名 二.归档方式对比 1. sele ...
- git学习之创建版本库(三)
创建版本库 什么是版本库呢?版本库又名仓库,英文名repository,你可以简单理解成一个目录,这个目录里面的所有文件都可以被Git管理起来,每个文件的修改.删除,Git都能跟踪,以便任何时刻都可以 ...
- 【Mysql】之视图操作
一.视图实例1-创建视图及查询数据操作 首先,创建三个表:user.course.user_course 表:user CREATE TABLE `user` ( `id` ) NOT NULL AU ...