LeetCode OJ 292.Nim Gam148. Sort List
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的更多相关文章
- 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 ...
- 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 ...
- 【Leetcode】292. Nim Game
problem 292. Nim Game solution class Solution { public: bool canWinNim(int n) { ; } }; 来generalize一下 ...
- 【一天一道LeetCode】#292. Nim Game
一天一道LeetCode 从今天开始,调整规律,不按顺序做,从easy开始! 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 ...
- 【LeetCode】292. Nim Game 解题报告(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 力扣(LeetCode)292. Nim游戏 巴什博奕
你和你的朋友,两个人一起玩 Nim游戏:桌子上有一堆石头,每次你们轮流拿掉 1 - 3 块石头. 拿掉最后一块石头的人就是获胜者.你作为先手. 你们是聪明人,每一步都是最优解. 编写一个函数,来判断你 ...
- 【Leetcode】292. Nim游戏
题目链接:https://leetcode-cn.com/problems/nim-game/description/ 您和您的朋友,两个人一起玩 Nim游戏:桌子上有一堆石头,每次你们轮流拿掉 1 ...
- 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 ...
- Leetcode题目292.Nim游戏(脑筋急转弯)
题目描述: 你和你的朋友,两个人一起玩 Nim 游戏:桌子上有一堆石头,每次你们轮流拿掉 1 - 3 块石头. 拿掉最后一块石头的人就是获胜者.你作为先手. 你们是聪明人,每一步都是最优解. 编写一个 ...
随机推荐
- iOS7之后的文本高度封装
#import "NSString+Util.h" @implementation NSString (Util) +(CGFloat)changeStationWidth:(NS ...
- wxWidgets显示视频
wxWidgets中似乎没有专门用于显示视频的控件(虽然有wxWidgets本身的openGL控件wxGLCANVAS,但是我没有去试--) 按照这篇文章所讲的,从wxPanel继承出一个DrawPa ...
- 树:BST、AVL、红黑树、B树、B+树
我们这个专题介绍的动态查找树主要有: 二叉查找树(BST),平衡二叉查找树(AVL),红黑树(RBT),B~/B+树(B-tree).这四种树都具备下面几个优势: (1) 都是动态结构.在删除,插入操 ...
- this的相关知识
一如既往,直接上代码: function fn(name,age){ var obj=new Object(); obj.name=name; obj.age=age; obj.talk=functi ...
- Android设置对话框去除黑边
在res/values/styles.xml文件中添加如下内容 <style name="dialog" parent="@android:style/Theme. ...
- php日期转时间戳,指定日期转换成时间戳
写过PHP+MySQL的程序员都知道有时间差,UNIX时间戳和格式化日期是我们常打交道的两个时间表示形式,Unix时间戳存储.处理方便,但 是不直观,格式化日期直观,但是处理起来不如Unix时间戳那么 ...
- Access denied for user 'root'@'localhost' (using password:YES) 解决方案[转]
关于昨天下午说的MySQL服务无法启动的问题,解决之后没有进入数据库,就直接关闭了电脑. 今早打开电脑,开始-运行 输入“mysql -uroot -pmyadmin”后出现以下错误: “Access ...
- IOS CrackMe 破解学习
一直在看别人如何破解一个app,下面自己也尝试着学习怎么去破解一个app的密码,下面是完整的过程. 准备工作: 一台mac或者pc安装了ssh客户端 一台越狱的iphone iphone上安装了ope ...
- PHP文本路径转换为链接文字
<?php /** * 文本路径转换为有链接的文字 * @param string $str 转换内容 * @return string */ function urlToLink($str) ...
- 单例:用AFNetworking封装举例
创建单例的方式 import AFNetworking class NetworkTools: AFHTTPSessionManager { //let是线程安全的 //使用单例实例只需要拿到Netw ...