【23. 合并K个排序链表】【困难】【优先队列/堆排序】
2024-09-06 07:19:28
合并 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;
}
}
}
}
最新文章
- 利用Python进行数据分析(1) 简单介绍
- linux虚拟机上不了网--桥接方式--问题一直未解决
- MediaWiki使用指南
- TextBox 英文文档
- C++中的虚函数(表)实现机制以及用C语言对其进行的模拟实现
- mysql出现“SELECT list is not in GROUP BY clause and contains nonaggregated column [duplicate]”错误提示
- C#使用ICSharpCode.SharpZipLib.dll压缩文件夹和文件
- js写个日历
- php大力力 [018节]如何联系大力力
- SQL SERVER 生成MYSQL建表脚本
- MySQL基于实例sales创建自定义函数、视图、存储过程及触发器
- css3 3D盒子效果
- android 屏幕适配1 ——dimens.xml的适配
- ofbiz学习笔记01--多表关联查询
- 【Alpha版本】冲刺阶段 - Day4 - 加速
- 小程序开发笔记【一】,查询用户参与活动列表 left join on的用法
- SpringBoot Web学习笔记
- Spring Boot + JPA 因为 javassist 包出现 NullPointerException 问题的解决
- mysql密码的坑
- ELKstack简介及环境部署
热门文章
- error C3861: “L”: 找不到标识符
- Openstack组件部署 — Networking service_Compute Node
- 分析无法进入Linux系统的原因
- POJ 3468 A Simple Problem with Integers (分块)
- UVA 10522 Height to Area(知三角形三高求面积)
- shell txt1写入txt2,并放在txt2中指定字符串的后面
- C++子类父类构造函数的关系
- leetcode.字符串.125验证回文串-Java
- print的简单使用
- gitnore文件修改生效方法