合并 k 个排序链表,返回合并后的排序链表。请分析和描述算法的复杂度。

示例:

输入:

[

1->4->5,

1->3->4,

2->6

]

输出: 1->1->2->3->4->4->5->6

/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public ListNode mergeKLists(ListNode[] lists) {
public class Solution {
public ListNode mergeKLists(List<ListNode> lists) {
Queue<ListNode> heap = new PriorityQueue(new Comparator<ListNode>(){
@Override public int compare(ListNode l1, ListNode l2) {
return l1.val - l2.val;
}
});
ListNode head = new ListNode(0), tail = head;
for (ListNode node : lists) if (node != null) heap.offer(node);
while (!heap.isEmpty()) {
tail.next = heap.poll();
tail = tail.next;
if (tail.next != null) heap.offer(tail.next);
}
return head.next;
}
}
}
}

最新文章

  1. 利用Python进行数据分析(1) 简单介绍
  2. linux虚拟机上不了网--桥接方式--问题一直未解决
  3. MediaWiki使用指南
  4. TextBox 英文文档
  5. C++中的虚函数(表)实现机制以及用C语言对其进行的模拟实现
  6. mysql出现“SELECT list is not in GROUP BY clause and contains nonaggregated column [duplicate]”错误提示
  7. C#使用ICSharpCode.SharpZipLib.dll压缩文件夹和文件
  8. js写个日历
  9. php大力力 [018节]如何联系大力力
  10. SQL SERVER 生成MYSQL建表脚本
  11. MySQL基于实例sales创建自定义函数、视图、存储过程及触发器
  12. css3 3D盒子效果
  13. android 屏幕适配1 ——dimens.xml的适配
  14. ofbiz学习笔记01--多表关联查询
  15. 【Alpha版本】冲刺阶段 - Day4 - 加速
  16. 小程序开发笔记【一】,查询用户参与活动列表 left join on的用法
  17. SpringBoot Web学习笔记
  18. Spring Boot + JPA 因为 javassist 包出现 NullPointerException 问题的解决
  19. mysql密码的坑
  20. ELKstack简介及环境部署

热门文章

  1. error C3861: “L”: 找不到标识符
  2. Openstack组件部署 — Networking service_Compute Node
  3. 分析无法进入Linux系统的原因
  4. POJ 3468 A Simple Problem with Integers (分块)
  5. UVA 10522 Height to Area(知三角形三高求面积)
  6. shell txt1写入txt2,并放在txt2中指定字符串的后面
  7. C++子类父类构造函数的关系
  8. leetcode.字符串.125验证回文串-Java
  9. print的简单使用
  10. gitnore文件修改生效方法