Sort a linked list in O(n log n) time using constant space complexity.

解题思路:

归并排序、快速排序、堆排序都是O(n log n),由于优先级队列是用堆排序实现的,因此,我们使用优先级队列即可,JAVA实现如下:

    public ListNode sortList(ListNode head) {
if(head==null||head.next==null)
return head;
Queue<ListNode> pQueue = new PriorityQueue<ListNode>(
0,new Comparator<ListNode>() {//提交的时候不能加初始容量(第一个参数),否则报错
public int compare(ListNode l1, ListNode l2) {
return l1.val - l2.val;
}
});
while(head!=null){
pQueue.add(head);
head=head.next;
}
head=pQueue.poll();
ListNode temp=head;
while(pQueue.size()>0){
temp.next=pQueue.poll();
temp=temp.next;
}
temp.next=null;
return head;
}

Java for LeetCode 148 Sort List的更多相关文章

  1. C#版 - LeetCode 148. Sort List 解题报告(归并排序小结)

    leetcode 148. Sort List 提交网址: https://leetcode.com/problems/sort-list/  Total Accepted: 68702 Total ...

  2. leetcode 148. Sort List ----- java

    Sort a linked list in O(n log n) time using constant space complexity. 排序,要求是O(nlog(n))的时间复杂度和常数的空间复 ...

  3. [LeetCode] 148. Sort List 链表排序

    Sort a linked list in O(n log n) time using constant space complexity. Example 1: Input: 4->2-> ...

  4. Java for LeetCode 075 Sort Colors

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

  5. [LeetCode] 148. Sort List 解题思路

    Sort a linked list in O(n log n) time using constant space complexity. 问题:对一个单列表排序,要求时间复杂度为 O(n*logn ...

  6. Java实现 LeetCode 148 排序链表

    148. 排序链表 在 O(n log n) 时间复杂度和常数级空间复杂度下,对链表进行排序. 示例 1: 输入: 4->2->1->3 输出: 1->2->3-> ...

  7. LeetCode 148 Sort List 链表上的归并排序和快速排序

    Sort a linked list in O(n log n) time using constant space complexity. 单链表排序----快排 & 归并排序 (1)归并排 ...

  8. Leetcode#148 Sort List

    原题地址 链表归并排序 真是恶心的一道题啊,哇了好多次才过. 代码: void mergeList(ListNode *a, ListNode *b, ListNode *&h, ListNo ...

  9. [LeetCode]148. Sort List链表归并排序

    要求时间复杂度O(nlogn),空间复杂度O(1),采用归并排序 传统的归并排序空间复杂度是O(n),原因是要用一个数组表示合并后的数组,但是这里用链表表示有序链表合并后的链表,由于链表空间复杂度是O ...

随机推荐

  1. 【POJ 2484】A Funny Game

    Description Alice and Bob decide to play a funny game. At the beginning of the game they pick n(1 &l ...

  2. MySql批处理的小窍门:排行榜类数据生成

    MySql批处理的小窍门:排行榜类数据生成 最近在做新版本的开发,其中涉及到排行榜的批量预生成,在此分享给大家. 关键点 名次的计算(不考虑用游标) 单榜单查询 对于排行榜这种类型的数据,当只查一个排 ...

  3. zoj3819Average Score

    Average Score Time Limit: 2 Seconds      Memory Limit: 65536 KB Bob is a freshman in Marjar Universi ...

  4. POJ1995(整数快速幂)

    http://poj.org/problem?id=1995 题意:求(A1^B1 + A2^B2 + .....Ah^Bh)%M 直接快速幂,以前对快速幂了解不深刻,今天重新学了一遍so easy ...

  5. JS 动画基础

    获取元素的样式 getStyle函数 function getStyle(element, attr) { if(element.currentStyle) { //针对IE return eleme ...

  6. FCK编辑器漏洞总结

    1.查看编辑器版本FCKeditor/_whatsnew.html————————————————————————————————————————————————————————————— 2. Ve ...

  7. html表单验证程序

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  8. TreeSet里面放对象,如果同时放入了父类和子类的实例对象,那比较时使用的是父类的compareTo方法,还是使用的子类的compareTo方法,还是抛异常!

    /** * * @author ocq */ class Parent implements Comparable { private int age = 0; public Parent(int a ...

  9. 转 Xenserver HVM is required for this operation的解决办法

    今天在XenServer中安装虚拟机时出现如下错误: 原因:没有开启XenServer服务器主机的虚拟化支持功能 解决办法:在XenServer主机的BIOS里开启CPU的虚拟化支持功能 本文出自 “ ...

  10. meta中的viewport指令

    网页手机wap2.0网页的head里加入下面这条元标签,在iPhone的浏览器中页面将以原始大小显示,并不允许缩放. <meta name="viewport" conten ...