LeetCode Sort List 链表排序(规定 O(nlogn) )
Status: Accepted
Runtime: 66 ms
题意:根据给出的单链表,用O(nlogn)的时间复杂度来排序。由时间复杂度想到快排、归并这两种排序。本次用的是归并排序。递归将链表的规模不断二分到只剩下1或2个元素为止,这也是递归出口,一旦出现这两种情况就可以返回。这里有个问题,链表也能二分?可以的,只是麻烦了些,用两个指针可以实现找到中点。本次代码没有详细分析具体的复杂度,但确实是归并。
注意:要考虑空链表,单个元素的链表,以及多个元素的链表。
LeetCode的代码:
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode *sorted(ListNode *head) {
//递归出口
if(!head->next) return head;
if(!head->next->next) //只有两个结点时就可以排序并返回了
{
if(head->val > head->next->val )
{
head->next->next=head;
head=head->next;
head->next->next=;
}
return head;
} //找中点。方法:p2每往后移2步,p1就移1步,当p2到达最后一个结点时,p1刚好在中间
ListNode *p1=head,*p2=head;
while(p2)
{
if(p2->next)
{
p2=p2->next->next; //允许p2为空,但若p2->next为空,则p2->next->next会出错
p1=p1->next;
}
else break;
}
ListNode * sec=p1->next; //左一半的最后一个结点的next必须为空,此举是必须的
p1->next=; //二分递归
ListNode * temp1=sorted(head);
ListNode * temp2=sorted(sec); //归并,且返回
ListNode * sorted_head=NULL,*sorted_rear=NULL,*jus=NULL;
if(temp1->val < temp2->val ) //初始化链表头尾
{
sorted_head=sorted_rear=temp1;
temp1=temp1->next;
jus=temp1; //jus摆在刚减少元素的那个链表,以方便判断是否该链表已没有元素了
}
else
{
sorted_head=sorted_rear=temp2;
temp2=temp2->next;
jus=temp2;
}
while(jus) //两个链表都非空
{
if(temp1->val < temp2->val)
{
sorted_rear->next=temp1;
temp1=temp1->next;
jus=temp1;
}
else
{
sorted_rear->next=temp2;
temp2=temp2->next;
jus=temp2;
}
sorted_rear=sorted_rear->next;
}
if(!temp1) //temp1为空
sorted_rear->next=temp2;
else
sorted_rear->next=temp1;
return sorted_head;
} ListNode *sortList(ListNode *head) {
if(!head) return ;
if(head->next==NULL) return head;
return sorted(head);
}
};
Sort list
可自己测试的代码:
#include <iostream>
using namespace std; struct ListNode {
int val;
ListNode *next;
}; ListNode *sorted(ListNode *head) {
//递归出口
if(!head->next) return head;
if(!head->next->next) //只有两个结点时就可以排序并返回了
{
if(head->val > head->next->val )
{
head->next->next=head;
head=head->next;
head->next->next=;
}
return head;
} //找中点。方法:p2每往后移2步,p1就移1步,当p2到达最后一个结点时,p1刚好在中间
ListNode *p1=head,*p2=head;
while(p2)
{
if(p2->next)
{
p2=p2->next->next; //允许p2为空,但若p2->next为空,则p2->next->next会出错
p1=p1->next;
}
else break;
}
ListNode * sec=p1->next; //左一半的最后一个结点的next必须为空,此举是必须的
p1->next=; //二分递归
ListNode * temp1=sorted(head);
ListNode * temp2=sorted(sec); //归并,且返回
ListNode * sorted_head=NULL,*sorted_rear=NULL,*jus=NULL;
if(temp1->val < temp2->val ) //初始化链表头尾
{
sorted_head=sorted_rear=temp1;
temp1=temp1->next;
jus=temp1; //jus摆在刚减少元素的那个链表,以方便判断是否该链表已没有元素了
}
else
{
sorted_head=sorted_rear=temp2;
temp2=temp2->next;
jus=temp2;
}
while(jus) //两个链表都非空
{
if(temp1->val < temp2->val)
{
sorted_rear->next=temp1;
temp1=temp1->next;
jus=temp1;
}
else
{
sorted_rear->next=temp2;
temp2=temp2->next;
jus=temp2;
}
sorted_rear=sorted_rear->next;
}
if(!temp1) //temp1为空
sorted_rear->next=temp2;
else
sorted_rear->next=temp1;
return sorted_head;
} ListNode *sortList(ListNode *head) {
if(!head) return ;
if(head->next==NULL) return head;
return sorted(head);
} int main()
{ ListNode * head=new(ListNode);
head->val=; //链表有1个元素
head->next=NULL;
ListNode * p=head;
ListNode * temp=NULL; //int a[11]={2,1,3,6,5,8,4,9,7,10};
int a[]={,}; //元素自己随意添加
for(int i=;i<;i++) //在这里控制元素个数。
{
temp=new(ListNode);
temp->val=a[i];
temp->next=NULL;
p->next=temp;
p=p->next;
}
p=sortList(head);
while(p)
{
printf("%d\n",p->val);
p=p->next;
} return ;
}
Test Code
LeetCode Sort List 链表排序(规定 O(nlogn) )的更多相关文章
- [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-> ...
- [LeetCode]147. Insertion Sort List链表排序
插入排序的基本思想 把排好的放在一个新的变量中,每次拿出新的,排进去 这个新的变量要有超前节点,因为第一个节点可能会有变动 public ListNode insertionSortList(List ...
- [LeetCode] Sort Colors 颜色排序
Given an array with n objects colored red, white or blue, sort them so that objects of the same colo ...
- [LintCode] Sort List 链表排序
Sort a linked list in O(n log n) time using constant space complexity. Have you met this question in ...
- LeetCode::Sort List 具体分析
Sort a linked list in O(n log n) time using constant space complexity. 这道题目非常简短的一句话.给链表排序,看到nlogn.我们 ...
- sort 树 hash 排序
STL 中 sort 函数用法简介 做 ACM 题的时候,排序是一种经常要用到的操作.如果每次都自己写个冒泡之类的 O(n^2) 排序,不但程序容易超时,而且浪费宝贵的比赛时间,还很有可能写错. ST ...
- Leetcode:148_Sort List | O(nlogn)链表排序 | Medium
题目:Sort List Sort a linked list in O(n log n) time using constant space complexity 看题目有两个要求:1)时间复杂度为 ...
- 148. Sort List (java 给单链表排序)
题目:Sort a linked list in O(n log n) time using constant space complexity. 分析:给单链表排序,要求时间复杂度是O(nlogn) ...
随机推荐
- Http客户端再封装
Android系统上推荐的Http客户端从Apache变成[HttpURLConnection],主要理由包括 * 不过因为UrlConnection这组接口时间较早(Java 1.0), 接口的设计 ...
- 网页游戏开发秘笈 PDF扫描版
精选10种常见的游戏类型,透过典型实例,深入剖析游戏引擎及工具的选用技巧,详细讲解每款游戏的制作过程,为快速掌握网页游戏开发提供系统而实用的指南. 网页游戏开发秘笈 目录: 译者序 前 言 导 言 ...
- 工作随记--div最小高度
给div添加最小高度 min-height:1000px;//IE7\FF height:100%;//IE6\IE7\FF 这个很重要,IE6定死高度后,需要再加上这条,才能自动延伸. _heigh ...
- 【Java面试题系列】:Java中final finally finalize的区别
本篇为[Java面试题系列]第三篇,文中如有错误,欢迎指正. 第一篇链接:[Java面试题系列]:Java基础知识常见面试题汇总 第一篇 第二篇链接:[Java面试题系列]:Java基础知识常见面试题 ...
- bzoj2724: [Violet 6]蒲公英(分块)
传送门 md调了一个晚上最后发现竟然是空间开小了……明明算出来够的…… 讲真其实我以前不太瞧得起分块,觉得这种基于暴力的数据结构一点美感都没有.然而今天做了这道分块的题才发现分块的暴力之美(如果我空间 ...
- 验证控件jQuery Validation Engine调用外部函数验证
在使用jQuery Validation Engine的时候,我们除了使用自带的API之外,还可以自己自定义正则验证.自定义正则验证上一篇已经讲过了,如果想使用自定义函数进行验证怎么办?其实这个控件有 ...
- windows系统IIS7环境下如何部署MVC项目
首先打开IIS:第一步:添加MVC程序映射 打开其中的:处理程序映射,如下图: 点击界面右边操作中的:添加脚本映射,弹出下图: 请求路径:* 可执行文件:c:/Windows/Mi ...
- [转]AFNetworking 3.0迁移指南
http://www.jianshu.com/p/047463a7ce9b?utm_campaign=hugo&utm_medium=reader_share&utm_content= ...
- 分层图最短路【bzoj2662】[BeiJing wc2012]冻结
分层图最短路[bzoj2662][BeiJing wc2012]冻结 Description "我要成为魔法少女!" "那么,以灵魂为代价,你希望得到什么?" ...
- php模拟post提交数据
$data = '{ "id": "17999030", "method": "sayHello", "jso ...