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) )的更多相关文章

  1. [LeetCode] Sort List 链表排序

    Sort a linked list in O(n log n) time using constant space complexity. 常见排序方法有很多,插入排序,选择排序,堆排序,快速排序, ...

  2. [LeetCode] 148. Sort List 链表排序

    Sort a linked list in O(n log n) time using constant space complexity. Example 1: Input: 4->2-> ...

  3. [LeetCode]147. Insertion Sort List链表排序

    插入排序的基本思想 把排好的放在一个新的变量中,每次拿出新的,排进去 这个新的变量要有超前节点,因为第一个节点可能会有变动 public ListNode insertionSortList(List ...

  4. [LeetCode] Sort Colors 颜色排序

    Given an array with n objects colored red, white or blue, sort them so that objects of the same colo ...

  5. [LintCode] Sort List 链表排序

    Sort a linked list in O(n log n) time using constant space complexity. Have you met this question in ...

  6. LeetCode::Sort List 具体分析

    Sort a linked list in O(n log n) time using constant space complexity. 这道题目非常简短的一句话.给链表排序,看到nlogn.我们 ...

  7. sort 树 hash 排序

    STL 中 sort 函数用法简介 做 ACM 题的时候,排序是一种经常要用到的操作.如果每次都自己写个冒泡之类的 O(n^2) 排序,不但程序容易超时,而且浪费宝贵的比赛时间,还很有可能写错. ST ...

  8. Leetcode:148_Sort List | O(nlogn)链表排序 | Medium

    题目:Sort List Sort a linked list in O(n log n) time using constant space complexity 看题目有两个要求:1)时间复杂度为 ...

  9. 148. Sort List (java 给单链表排序)

    题目:Sort a linked list in O(n log n) time using constant space complexity. 分析:给单链表排序,要求时间复杂度是O(nlogn) ...

随机推荐

  1. ASP.NET MVC 小牛之旅1:何谓MVC

    在学习ASP.NET MVC之前首先了解什么 是MVC ? MVC不是一种语言,严格来说也不算一个技术,而是开发时所使用的一种架构(框架),它就像是一种开发观念,或是一个设计样式. MVC让软件开发的 ...

  2. <你的孤独,虽败犹荣> 很喜欢的句子

    希望未来的工作中能够经常出差,做一个能看到除了湖南之外的世界的人 即使我们一辈子给人打工,也要打自己愿意打的工 正在经历的孤独,我们称之为迷茫,经过的那些孤独,我们称之为成长 青春,是一个容量极其有限 ...

  3. CAS客户端整合(三) Otrs

    OTRS 是用Perl写的一个工单邮件系统,非常强大. 登录流程 流程图略过 otrs没有像 discuz 和 zabbix 类似的游客登录状态,这样处理起来逻辑分支少一些. 不过还是考虑用 otrs ...

  4. 《OD学hadoop》20160910某旅游网项目实战

    一.event事件分析 叶子节点只计算一次 父节点的触发次数由子节点的数量节点 事件流:是由业务人员定义的一系列的具有前后顺序的事件构成的用户操作行为,至少包括两个事件以上. 目标:以事件流为单位,分 ...

  5. MCP|XHK|High-density peptide arrays help to identify linear immunogenic B cell epitopes in individuals naturally exposed to malaria infection(高密度肽段阵列有助于在自然暴露于疟疾感染的个体中识别线性免疫原性B细胞表位)

    文献名:High-density peptide arrays help to identify linear immunogenic B cell epitopes in individuals n ...

  6. Redis数据类型,持久化,回收策略——(Redis缓存第一章)

    缓存:第一种是内存缓存 比如Map(简单的数据结构),以及EH Cache(Java第三方库),第二种是缓存组件比如Memached,Redis:Redis(remote dictionary ser ...

  7. 高斯消元法的C++简单实现

    高斯消元法 首先,我们导入几个概念. 定义1: 一个矩阵称为阶梯形(行阶梯形),若它有以下三个性质: 1.每一非零行在每一零行之上: 2.某一行的先导元素所在的列位于前一行先导元素的后面: 3.某一行 ...

  8. jsp内置对象request的使用方法

    <%@page import="java.text.SimpleDateFormat"%> <%@page import="java.util.Date ...

  9. Jenkins +Maven+Tomcat+SVN +Apache项目持续集成构建

    详解Jenkins +Maven+Tomcat+SVN +Apache项目持续集成 一:前言 1. Jenkins jenkins版本大全http://mirrors.jenkins-ci.org/ ...

  10. 配置文件中取值: spring配置文件中util:properties和context:property-placeholder

    转载大神 https://blog.csdn.net/n447194252/article/details/77498916 util:properties和context:property-plac ...