Sort List (使用归并排序的链表排序)
Sort a linked list in O(n log n) time using constant space complexity.
C++代码的实现:
#include<iostream>
#include<new>
using namespace std; //Definition for singly-linked list.
struct ListNode
{
int val;
ListNode *next;
ListNode(int x) : val(x), next(NULL) {}
};
class Solution
{
public:
ListNode *sortList(ListNode *head)
{
if(head==NULL)
return NULL;
//递归的结束条件,注意,与对数组的归并排序不同
if(head->next==NULL)
return head;
ListNode *p=head;
//q不能从head开始,为什么呢?
ListNode *q=head->next;
ListNode *l2=NULL;
while(q&&q->next)
{
p=p->next;
q=q->next->next;
}
l2=p->next;
p->next=NULL;
return Merge(sortList(head),sortList(l2));
}
ListNode *Merge(ListNode *l1,ListNode *l2)
{
ListNode *pre=l1;
ListNode *p=l1;
ListNode *q=l2;
while(p&&q)
{
if(p->val<=q->val)
{
pre=p;
p=p->next;
continue;
}
else
{
l2=q->next;
q->next=NULL;
q->next=p;
if(p==l1)
l1=q;
else
pre->next=q;
pre=q;
q=l2;
}
}
if(q)
pre->next=q;
return l1;
}
void createList(ListNode *&head)
{
ListNode *p=NULL;
int i=;
int arr[]= {,,,,,,,,,};
for(i=; i<; i++)
{
if(head==NULL)
{
head=new ListNode(arr[i]);
if(head==NULL)
return;
}
else
{
p=new ListNode(arr[i]);
p->next=head;
head=p;
}
}
}
}; int main()
{
Solution s;
ListNode *L=NULL;
s.createList(L);
ListNode *L1=NULL;
L1=s.sortList(L);
while(L1)
{
cout<<L1->val<<" ";
L1=L1->next;
}
}
运行结果:

参考:http://www.tuicool.com/articles/2eemi2
http://www.cnblogs.com/tenosdoit/p/3666585.html
Sort List (使用归并排序的链表排序)的更多相关文章
- 148. Sort List (java 给单链表排序)
题目:Sort a linked list in O(n log n) time using constant space complexity. 分析:给单链表排序,要求时间复杂度是O(nlogn) ...
- insertion sort list (使用插入排序给链表排序)
Sort a linked list using insertion sort. 对于数组的插入排序,可以参看排序算法入门之插入排序(java实现),遍历每个元素,然后相当于把每个元素插入到前面已经排 ...
- [LeetCode] Sort List 链表排序
Sort a linked list in O(n log n) time using constant space complexity. 常见排序方法有很多,插入排序,选择排序,堆排序,快速排序, ...
- [LeetCode] 148. Sort List 链表排序
Sort a linked list in O(n log n) time using constant space complexity. Example 1: Input: 4->2-> ...
- 给乱序的链表排序 · Sort List, 链表重排reorder list LoLn...
链表排序 · Sort List [抄题]: [思维问题]: [一句话思路]: [输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入): [画图]: quick ...
- Leetcode:148_Sort List | O(nlogn)链表排序 | Medium
题目:Sort List Sort a linked list in O(n log n) time using constant space complexity 看题目有两个要求:1)时间复杂度为 ...
- C语言 链表的使用(链表的增删查改,链表逆转,链表排序)
//链表的使用 #define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<stdlib.h> #include< ...
- 八大排序方法汇总(选择排序,插入排序-简单插入排序、shell排序,交换排序-冒泡排序、快速排序、堆排序,归并排序,计数排序)
2013-08-22 14:55:33 八大排序方法汇总(选择排序-简单选择排序.堆排序,插入排序-简单插入排序.shell排序,交换排序-冒泡排序.快速排序,归并排序,计数排序). 插入排序还可以和 ...
- c语言:链表排序, 链表反转
下面将实现链表排序的排序和遍历显示功能: 所定义的链表结构如下: head -> p1 -> p2 ->p3 ->....->pn; head的本身不作为数据节点,hea ...
随机推荐
- angular.js学习手册(二)
如何使用angularjs? 各个 angular.js 版本下载: https://github.com/angular/angular.js/releases 下载完之后,在你需要使用angula ...
- Java数据库连接池的几种配置方法(以MySQL数据库为例)
Java数据库连接池的几种配置方法(以MySQL数据库为例) 一.Tomcat配置数据源: 前提:需要将连接MySQL数据库驱动jar包放进Tomcat安装目录中common文件夹下的lib目录中 1 ...
- 设置windows窗口ICON 【windows 编程】【API】【原创】
1. ICON介绍 最近开始接触windows 编程,因此将自己所接触的一些零散的知识进行整理并记录.本文主要介绍了如何更改windows对话框窗口的ICON图标.这里首先介绍一下windows IC ...
- FMDB警告Warning: there is at least one open result set around after performing的问题
FMDB操作sqlite的时候总是报警告Warning: there is at least one open result set around after performing,后来发现是执行查询 ...
- 插入数据显示 Duplicate entry '4913' for key 'user_id'
提示信息翻译:插入4913 使索引重复 分析:索引如果是primary unique这两两种,那么数据表的数据对应的这个字段就必须保证其每条记录的唯一性.否则就会产生这个错误. 一般发生在对数据库写操 ...
- hdu 1241 Oil Deposits(DFS求连通块)
HDU 1241 Oil Deposits L -DFS Time Limit:1000MS Memory Limit:10000KB 64bit IO Format:%I64d & ...
- Ant快速入门(三)-----定义生成文件
适应Ant的关键就是编写生成文件,生成文件定义了该项目的各个生成任务(以target来表示,每个target表示一个生成任务),并定义生成任务之间的依赖关系. Ant生成文件的默认名为build.xm ...
- 转:Google论文之三----MapReduce
文章来自于:http://www.cnblogs.com/geekma/p/3139823.html MapReduce:大型集群上的简单数据处理 摘要 MapReduce是一个设计模型,也是一个处理 ...
- poj 3308 Paratroopers
http://poj.org/problem?id=3308 #include <cstdio> #include <cstring> #include <algorit ...
- Circular placeholder reference 'server.port' in property definitions
Exception in thread "main" java.lang.IllegalArgumentException: Circular placeholder refere ...