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-产生随机数

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

  2. MySql循环语句

    drop procedure if exists test_insert; DELIMITER ;; CREATE PROCEDURE test_insert () BEGIN DECLARE i I ...

  3. mpvue最佳实践 , 美团出的一个小程序框架

    看手机微信,看到说美团出了1个小程序框架,  mpvue 搜下来试试,看了网上的一个对比 ----------------- 以下为引用 我们对微信小程序.mpvue.WePY 这三个开发框架的主要能 ...

  4. JavaScript判断当前手机是Android还是iOS系统

    $(function () { var u = navigator.userAgent, app = navigator.appVersion; var isAndroid = u.indexOf(' ...

  5. Win10 开启便签快捷键

    前提: Win10系统 方法: 开启便签的快捷键:windows+W 这样就可以打开便签,使用起来非常方便

  6. CSS样式----css样式表和选择器(图文详解)

    本文最初于2015-10-03发表于博客园,并在GitHub上持续更新前端的系列文章.欢迎在GitHub上关注我,一起入门和进阶前端. 本文主要内容 CSS概述 CSS和HTML结合的三种方式:行内样 ...

  7. Windows 自动更新服务恢复

    之前手贱删除了Windows的自动更新服务,命令: SC DELETE Wuauserv 悲剧的是最近中了[永恒之蓝]病毒,很恼人!杀了毒,最后还是得仰仗Windows的补丁来加固系统.于是想通过SC ...

  8. ionic默认样式android和ios差异

    ionicframework中android和ios在默认样式上有一些不同的地方,官方文档中都有说明,但是经常会想不起. 一.差异: 1.tab位置,$ionicConfigProvider, tab ...

  9. CSS揭秘(一)引言

    借了一本CSS揭秘,国外的一本书,应该是目前相关书目里最好的了,内容非常扎实,不得不说图灵教育出的书真的不错,不然不是很厚的一本书卖到99也是.... 国外的这类书总是以问题开始,然后通过解决问题引出 ...

  10. 获取本地机器的特征码IP MAC

    using System; using System.Collections.Generic; using System.Text; using System.Net; using System.Ma ...