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. redis34--string 操作

    String类型操作 1.set key value 设置key对应的值为string类型的value  2.mset key1 value1 - keyN valueN 一次设置多个key的值 3. ...

  2. Strusts2--课程笔记9

    防止表单重复提交: 用户可能由于各种原因,对表单进行重复提交.Struts2中使用令牌机制防止表单自动提交.以下引用自北京动力节点: 前端表单提交代码: <%@ page language=&q ...

  3. Chapter 2 Open Book——31

    "It's too bad about the snow, isn't it?" Edward asked. I had the feeling that he was forci ...

  4. Dubbo入门实例 本地伪集群测试Demo

    1.   概述 Dubbo是一个分布式服务框架,致力于提供高性能和透明化的RPC远程服务调用方案,以及SOA服务治理方案 Dubbo是阿里巴巴SOA服务化治理方案的核心框架,每天为2,000+个服务提 ...

  5. 《JS权威指南学习总结--9.2 类和构造函数》

    内容要点: 例9-1展示了在JS中定义类的其中一种方法.但这种方法并不常用,毕竟它没有定义构造函数,构造函数是用来初始化新创建的对象的. 使用关键字new来调用构造函数会自动创建一个新对象,因此构造函 ...

  6. 【Python@Thread】queue模块-生产者消费者问题

    python通过queue模块来提供线程间的通信机制,从而可以让线程分项数据. 个人感觉queue就是管程的概念 一个生产者消费者问题 from random import randint from ...

  7. 更改自身web项目的图标(默认为tomcat的小喵咪)

    在页面<head>标签中加入 <link rel="shortcut icon" href="img/11.png" type="i ...

  8. DD应用实例

    1.将本地的/dev/hdb整盘备份到/dev/hdd dd if=/dev/hdb of=/dev/hdd2.将/dev/hdb全盘数据备份到指定路径的image文件dd if=/dev/hdb o ...

  9. hdu_5810_Balls and Boxes(打表推公式)

    题目链接:hdu_5810_Balls and Boxes 题意: 如题,让你求那个公式的期望 题解: 打表找规律,然后推公式.这项技能必须得学会 #include<cstdio> #in ...

  10. OpenCV2.x自学笔记——自适应阈值

    adaptiveThreshold(src,dst, double maxValue, int adaptiveMethod, int thresholdType, int blockSize, do ...