Sort a linked list in O(n log n) time using constant space complexity.

排序问题是我们遇到的一个老问题,从大一开始我们就学习了各种排序算法,大部分是基于数组的,比如冒泡排序,插入排序,快速排序等。这其中冒泡排序,插入排序的算法复杂度都是O(n2),插入排序的时间复杂度为O(nlogn)。对于这个题目,显然我们不能使用冒泡排序和插入排序,因为这样时间复杂度不满足要求,那么链表适合用快速排序吗?答案是肯定的,链表的结构还是非常方便使用快速排序的。

快速排序的特点是:如果待排序的数字列表越散乱,效果越好。如果数字列表是完全升序或者完全降序,快排的效果最差。最好情况下时间复杂度为O(nlogn),最坏情况为O(n2)。

快速排序的思想是:1.选一个key值;2.把小于key值的数移到key的左边;3.把大于key值的数移到key的右边;4.对左半部分和右半部分使用快速排序算法。

在对链表进行快速排序时,我们可以选择头结点作为key值,然后遍历链表,把比头结点val值小的节点和比头结点val值大的节点拆分到两个链表中。然后对这两个链表分别进行快速排序,然后把这三部分组装起来就好了。代码如下:

 public class Solution {
public ListNode sortList(ListNode head) {
if(head==null || head.next==null) return head; ListNode lefthead = null;
ListNode righthead = null;
ListNode mid = head;
head = head.next;
mid.next = null;
ListNode midtail = mid; while(head!=null){ //把链表拆分成三部分
ListNode temp = head;
head = head.next;
if(temp.val < mid.val){ //比head.val小的部分
temp.next = lefthead;
lefthead = temp;
}
else if(temp.val == mid.val){
temp.next = mid;
mid = temp;
}
else{
temp.next = righthead; //比head.val大的部分
righthead = temp;
}
} righthead = sortList(righthead);//把三部分组装起来
if(lefthead==null){
midtail.next = righthead;
return mid;
}
lefthead = sortList(lefthead); ListNode tmp = lefthead;
while(tmp.next!=null) tmp = tmp.next;
tmp.next = mid;
midtail.next = righthead;
return lefthead;
}
}

LeetCode OJ 292.Nim Gam148. Sort List的更多相关文章

  1. LeetCode OJ 292.Nim Game

    You are playing the following Nim Game with your friend: There is a heap of stones on the table, eac ...

  2. LeetCode OJ 292.Nim Gam19. Remove Nth Node From End of List

    Given a linked list, remove the nth node from the end of list and return its head. For example, Give ...

  3. 【Leetcode】292. Nim Game

    problem 292. Nim Game solution class Solution { public: bool canWinNim(int n) { ; } }; 来generalize一下 ...

  4. 【一天一道LeetCode】#292. Nim Game

    一天一道LeetCode 从今天开始,调整规律,不按顺序做,从easy开始! 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 ...

  5. 【LeetCode】292. Nim Game 解题报告(Java & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  6. 力扣(LeetCode)292. Nim游戏 巴什博奕

    你和你的朋友,两个人一起玩 Nim游戏:桌子上有一堆石头,每次你们轮流拿掉 1 - 3 块石头. 拿掉最后一块石头的人就是获胜者.你作为先手. 你们是聪明人,每一步都是最优解. 编写一个函数,来判断你 ...

  7. 【Leetcode】292. Nim游戏

    题目链接:https://leetcode-cn.com/problems/nim-game/description/ 您和您的朋友,两个人一起玩 Nim游戏:桌子上有一堆石头,每次你们轮流拿掉 1  ...

  8. LeetCode OJ:Nim Game(Nim游戏)

    You are playing the following Nim Game with your friend: There is a heap of stones on the table, eac ...

  9. Leetcode题目292.Nim游戏(脑筋急转弯)

    题目描述: 你和你的朋友,两个人一起玩 Nim 游戏:桌子上有一堆石头,每次你们轮流拿掉 1 - 3 块石头. 拿掉最后一块石头的人就是获胜者.你作为先手. 你们是聪明人,每一步都是最优解. 编写一个 ...

随机推荐

  1. 阿里云 CentOS7.2 配置FTP+Node.js环境

    本人小白,写下这篇博客意在记录踩过的坑,大神请绕道~ 准备工作 安装自己喜欢的连接软件(一般是putty或者xshell),本人选择的是xshell,软件如图 : 通过软件中的ssh连接连接上已经购买 ...

  2. mysql索引类型-形式-使用时机-不足之处--注意事项

    一.索引的类型 1.普通索引   增加 create  index  index_name on table(colume(length));                       例子:cre ...

  3. CodeForces 702B Powers of Two

    简单题. 开一个$map$记录一下每个数字出现了几次,那么读入的时候$f[a[i]]+1$. 计算$a[i]$做出的贡献的时候,先把$f[a[i]]-1$,然后再枚举$x$,答案加上$f[{2^x} ...

  4. php:跨域

    一个没那么难的历史难题,其实只要在被请求端,加一句: header('Access-Control-Allow-Origin: *'); 然后--然后没有了. //跨域访问的时候才会存在此字段 $or ...

  5. jQuery判断滚动条是上滚还是下滚,且是否到达底部或顶部

    jQuery判断滚动条是上滚还是下滚,且是否到达底部或顶部:http://www.haorooms.com/post/jquery_scroll_upanddown //滚动条滚动加载更多内容 //判 ...

  6. (转)eclipse自动补全的设置

    如果你用过Visual Studio的自动补全功能后,再来用eclipse的自动补全功能,相信大家会有些许失望. 但是eclipse其实是非常强大的,eclipse的自动补全没有VS那么好是因为ecl ...

  7. Linux入门(五)linux服务器文件远程管理

     1 使用filezila远程管理linux服务器文件 filezila下载地址:https://filezilla-project.org/ filezila默认只能登录普通用户,如果想要root用 ...

  8. hdu_5858_Hard problem(数学题)

    题目链接:hdu_5858_Hard problem 题意: 让你求阴影部分面积 题解: 推推公式就行 #include<stdio.h> #include<math.h> # ...

  9. 二维小波包分解wpdec2

    load woman; %小波包2尺度(层)分解 t=wpdec2(X,2,'haar'); plot(t);%绘制小波包树 %提取(1,2)处结点.也可以点击上图结点,观察 c12=wpcoef(t ...

  10. vultr机房vps价格20%优惠,赶紧来抢!

    vps服务商vultr全线优惠促销!价格降幅是20%,强烈推荐站长们留意. vps猫腻很多,我写过大量文章介绍vps的优点和几大厂商情况.友情提醒,千万不要采购国内所谓vps云主机,你的数据很不安全哟 ...