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. ProtoBuf练习(三)

    任意类型 protobuf语言的任意字段类型相当于Boost库的boost::any类型数据,google.protobuf.Any是对protobuf语言的message进行封装,所以需要使用mes ...

  2. Pillow的安装和使用

    需要把一段文字转换成图片,我找到了PIL(Python Imaging Library)库,专门干这个用的.还有一个Pillow是“friendly PIL fork”,于是我选择了后者. 安装过程稍 ...

  3. Mybatis中文模糊查询,数据库中有数据,但无结果匹配

    1.Mybatis中文模糊查询,数据库中有数据,但无结果匹配 1.1 问题描述: Mybatis采用中文关键字进行模糊查询,sql语句配置无误,数据库有该数据,且无任何报错信息,但无查询结果 1.2 ...

  4. 前端页面唯一字符串生成(Js)UUID

    function uuid() { var s = []; var hexDigits = "0123456789abcdef"; for (var i = 0; i < 3 ...

  5. 洛谷P1038 神经网络

    P1038 神经网络 题目背景 人工神经网络(Artificial Neural Network)是一种新兴的具有自我学习能力的计算系统,在模式识别.函数逼近及贷款风险评估等诸多领域有广泛的应用.对神 ...

  6. 使用mount命令挂载CDROM

    Linux显示所有的目录都在一个目录树下,而于他们位于哪一个驱动器/硬件无关.在Linux下的磁盘内容作为子目录形式出现的.可移动介质的内容不会自动出现在这些自目录的,我们必须通过挂载驱动器来实现. ...

  7. Jmeter中JDBC Request和BeanShell PostProcessor的结合使用(SQL模糊查询)

    [前言] 今天记录一下Jmeter中JDBC Request和BeanShell PostProcessor的结合使用的方法(SQL模糊查询) [步骤] 1.下载对应数据库的驱动包到jmeter安装目 ...

  8. 牛客练习赛43F(推式子)

    要点 题目链接 1e18的数据无法\(O(n)\)的容斥,于是推式子,官解,其中式子有点小错误 不必预处理mu,直接按照素数的个数判断正负即可 #include <bits/stdc++.h&g ...

  9. 强制更新客户端Silverlight XAP 文件

    在发布小程序更新的时候访问的总是原来的程序,猜想应该是缓存的原因.在网上查找方法 <div id="silverlightControlHost"> <objec ...

  10. ssh无需密码登录linux服务器

    使用下例中ssky-keygen和ssh-copy-id,仅需通过3个步骤的简单设置而无需输入密码就能登录远程Linux主机. ssh-keygen 创建公钥和密钥. ssh-copy-id 把本地主 ...