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 ...
随机推荐
- loop_login.sh
[oracle@ora10g scripts]$ cat loop_login.sh #/bin/bash####################export ORACLE_BASE=/u01/app ...
- 【转】Auto Layout 进阶
原文:http://blog.csdn.net/ysy441088327/article/details/12558097 引言: Auto Layout是iOS6发布后引入的一个全新的布局特性, ...
- js 点名
<!doctype html> <html> <head> <meta charset="utf-8"> <title> ...
- highcharts实例教程一:结合php与mysql生成折线图
Highcharts是一款纯javascript和html5编写的图表库,不仅几乎能兼容所有pc浏览器,而且对ios和android手机端的兼容 性也不错,它能够很简单便捷的在Web网站或Web应用中 ...
- Codeforces Round #313 A Currency System in Geraldion
A Currency System in Geraldion Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64 ...
- easyui tabs 真正刷新用法
//刷新当前标签Tabs function RefreshTab(currentTab) { var url = $(currentTab.panel('options')).attr('href') ...
- MATLAB文件操作及读txt文件
转自:http://blog.csdn.net/vblittleboy/article/details/8049748 文件操作是一种重要的输入输出方式,即从数据文件读取数据或将结果写入数据文件.MA ...
- Raspberry PI Model B+ (LCD显示CPU温度)
Title:Raspberry PI Model B+ (LCD显示CPU温度) --2015-01-29 17:44 买了块连接Raspberry PI Model B+的LCD显示器,上面没写C ...
- hadoop安装问题记录
start-yarn.sh 启动正常,但是无法访问网页http://localhost:8088/cluster 原因: 可能是ipv6 的问题 解决方法: http://stackoverflow. ...
- RandomAccessFile和memory-mapped files
[0]README 0.1) 本文描述转自 core Java volume 2, 旨在理解 java流与文件——RandomAccessFile类解析 的相关知识: 0.1) 本文 转自: http ...