1.题目描述:

2.解题思路:

  本题是要堆一个链表进行排序,并且要求时间复杂度为 O(n log n)。很明显,要用到分治的思想,用二分法进行归并排序:找到链表的middle节点,然后递归对前半部分和后半部分分别进行归并排序,最后对两个已排好序的链表进行Merge。

  分为三步:

  (1)找到中间结点,将链表分割成两部分。这里用到快慢两个指针的方法。

  (2)对前后每一部分分别进行归并排序。这里用到递归。

  (3)对两个已排好序的链表进行合并。这里用到前面merge 2 list的方法。

  

3.Java代码:

//public class LeetCode148 为测试代码
public class LeetCode148 {
public static void main(String[] args) {
ListNode n1=new ListNode(3),n2=new ListNode(2),n3=new ListNode(1),n4=new ListNode(5);
ListNode p=n1;
n1.next=n2;
n2.next=n3;
n3.next=n4;
System.out.print("初始时链表:"+p.val);
while(p.next!=null){
System.out.print("->"+p.next.val);
p=p.next;
}
System.out.println();
ListNode head=new Solution().sortList(n1);
System.out.print("排序后链表:"+head.val);
while(head.next!=null){
System.out.print("->"+head.next.val);
head=head.next;
}
}
} //class Solution为ac代码
class Solution {
public ListNode sortList(ListNode head) {
if(head==null||head.next==null) return head;
// step 1. 将链表分割为两部分
ListNode pre=null,slow=head,fast=head;
while(fast!=null&&fast.next!=null){
pre=slow;//pre指针是为了记住slow的前一个节点位置
slow=slow.next;
fast=fast.next.next;
}
pre.next=null;//将链表从pre和slow之间断开
// step 2. 对每一部分进行排序
ListNode l1=sortList(head);
ListNode l2=sortList(slow);
// step 3. merge l1 and l2
return merge2Lists(l1,l2);
} private ListNode merge2Lists(ListNode l1, ListNode l2) {
ListNode fakeHead=new ListNode(0);
ListNode p=fakeHead;
while(l1!=null&&l2!=null){
if(l1.val<l2.val){
p.next=l1;
l1=l1.next;
}else{
p.next=l2;
l2=l2.next;
}
p=p.next;
}
if(l1!=null) p.next=l1;
if(l2!=null) p.next=l2;
return fakeHead.next;
}
}
class ListNode{
int val;
ListNode next;
ListNode(int x) { val = x; }
}

测试结果:

思考:我将merge2Lists方法写成递归的写法时,即

 private ListNode merge2Lists(ListNode l1, ListNode l2) {
if(l1==null) return l2;
if(l2==null) return l1;
if(l1.val<l2.val){
l1.next=merge2Lists(l1.next, l2);
return l1;
}else{
l2.next=merge2Lists(l1, l2.next);
return l2;
}
}

这时在eclipse上测试完全没问题,但是在LeetCode上提交不通过,至今没想明白说明问题,如果哪位大神明白为什么,一定告诉我。。。

【LeetCode148】Sort List★★bug的更多相关文章

  1. 【LeetCode】Sort Colors 解题报告

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

  2. 【LeetCode】Sort Colors

    Sort Colors Given an array with n objects colored red, white or blue, sort them so that objects of t ...

  3. 【leetcode】Sort Colors(middle)☆

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

  4. 【leetcode】Sort List (middle)

    Sort a linked list in O(n log n) time using constant space complexity. 思路: 用归并排序.设输入链表为S,则先将其拆分为前半部分 ...

  5. 【leetcode】Sort List

    Sort List Sort a linked list in O(n log n) time using constant space complexity.   需要采用归并排序对链表进行操作. ...

  6. 【shell】sort命令

    [root@andon ~]# sort 1 ##常用正序自动排序 101 paul 18 100 102 suan 11 99 103 peter 18 98 id name age score [ ...

  7. 【Leetcode】Sort List JAVA实现

    Sort a linked list in O(n log n) time using constant space complexity. 1.分析 该题主要考查了链接上的合并排序算法. 2.正确代 ...

  8. 【原】从一个bug浅谈YUI3组件的资源加载

    篇前声明:为了不涉及业务细节,篇内信息统一以某游戏,某功能代替 前不久,某游戏准备内测客户端,开发人员测试过程中发现某功能突然不灵了,之前的测试一切ok,没有发现任何异常,第一反应是,游戏内浏览器都是 ...

  9. 【POJ】2492 A bug's life ——种类并查集

    A Bug's Life Time Limit: 10000MS   Memory Limit: 65536K Total Submissions: 28211   Accepted: 9177 De ...

随机推荐

  1. 【代码笔记】iOS-performSelector

    代码: - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. se ...

  2. js 回调函数理解与应用

    定义:在JavaScript中,回调函数具体的定义为:函数A作为参数(函数引用)传递到另一个函数B中,并且这个函数B执行函数A.我们就说函数A叫做回调函数.如果没有名称(函数表达式),就叫做匿名回调函 ...

  3. JavaScript判断变量名是否存在数组中

    直接上代码: JavaScript代码: var array=[{name:"张珊",sex:"男"}]; console.log(array); if(arr ...

  4. AWS CSAA -- 04 AWS Object Storage and CDN - S3 Glacier and CloudFront(二)

    015 Version Control - Lab 016 Cross Region Replication 017 Lifecycle Management  Glacier - Lab 018 C ...

  5. 域名检索&路由算法

    域名查询顺序: a. 浏览器缓存(本机hosts文件),浏览器会缓存DNS记录一段时间. b. 系统缓存 c. 路由器缓存 d. 检查ISP e. 递归搜索域名服务器 路由算法: 一.静态路由算法 a ...

  6. python虚拟环境 -- virtualenv , virtualenvwrapper

    virtualenv -- python虚拟沙盒 有人说:virtualenv.fabric 和 pip 是 pythoneer 的三大神器. 一.安装 pip install virtualenv ...

  7. Will Georgia Tech's $7K online M.S. in computer science program make the grade?

    https://newatlas.com/georgia-tech--graduate-computer-science-degree-mooc/28763/ Georgia Tech to offe ...

  8. Java 设计模式笔记

    0. 说明 转载 & 参考大部分内容 JAVA设计模式总结之23种设计模式 1. 什么是设计模式 设计模式(Design pattern)是一套被反复使用.多数人知晓的.经过分类编目的.代码设 ...

  9. csrf在web网站中有多重要

    小弟是学python的,今天在上网时看到一个商城网站,正好昨天学到了CSRF跨站请求,就对这个商城网站进行了一波测试 可以看到网页布局做的还是很不错的,然后进入了注册页面看看 之后就开始测试了 正常请 ...

  10. mac下idea 13 在tomcat 7控制台乱码

    在mac或linux下idea 13(可能其它版本也会出现乱码) tomcat 7在输出到控制台的日志中文乱码,解决方式 加一个environment variable, 在如图绿色位置添加   JA ...