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. CSS文字超出指定长度,用省略号

    overflow:hidden; text-overflow:ellipsis; white-space:nowrap; display: block;

  2. Unity Profiler CPU Usage(CPU使用情况)

    在Profiler界面点击左侧CPU Usage,Profiler界面下方Hierarchy窗口会列出各个函数对当前CPU的耗时,从大到小排序. 然后分析,各个函数的耗时是否异常,分析有没有可以优化的 ...

  3. mongodb3.X权限配置

    环境: CentOS6.8  mongodb3.4.1 1.连接mongodb数据库(如果mongo命令没有做环境变量配置,需要定位到有mongo命令的目录) [root@VM_118_34_cent ...

  4. Java标识符

    相关内容: JAVA标识符: 定义 组成规则 常见的命名规则 包 类和接口 方法.变量 常量 首发时间:2017-06-22 20:40 修改时间: 2018-03-16 14:01 :修改了标题,修 ...

  5. 从零自学Java-5.使用条件测试进行判断

    1.使用if语句进行最基本的条件测试:2.测试一个值大于还是小于另一个值:3.测试两个值是否相等:4.使用与if语句对应的else语句:5.组合多个条件测试:6.使用switch语句进行复杂的条件测试 ...

  6. qq会员权益

    1.功能特权qq会员可以获得增加好友上限.QQ等级加速.创建2000人群.创建1000人群.表情漫游.云消息服务.离线传文件.网络相册.靓号抵用卷.文件中转站这10个方面的福利当然会员和超级会员在上面 ...

  7. python新生类和经典类简单说明

    经典类: #!/usr/bin/env python #*-* coding:utf-8 *-* class A(): def __init__(self): print 'my name is GF ...

  8. CSS| table property

  9. .Net WebRequest异步请求与WebClient异步请求

    很多情况下一般会使用同步方式发出请求,直到响应后再做后续的逻辑处理等,但有时候后续的逻辑处理不依赖于请求的结果或者是可以挂起等到响应后再处理,又或者是为了解决UI“假死”的现象,这时可以使用异步请求 ...

  10. od 转储 二进制文件常用命令

    od :  NAME od - dump files in octal and other formats 常用命令: ➜ Downloads od -t x1 -Ax /etc/ld.so.cach ...