当前位置:网站首页>LeetCode 23: 合并K个升序链表
LeetCode 23: 合并K个升序链表
2022-08-02 03:57:00 【斯沃福德】
题目:
思路:
使用最小堆,for-each 取出每一个链表的头节点,将val值依次放入堆中;
最后从小到大依次取出即可;
使用哨兵节点;
class Solution {
PriorityQueue<Integer> p=new PriorityQueue<>();
public ListNode mergeKLists(ListNode[] lists) {
// 拿到每个listNode的头节点
for(ListNode k:lists){
add(k);
}
ListNode mer=new ListNode(-1);
ListNode curr=mer;
while(!p.isEmpty()){
int temp=p.poll();
curr.next=new ListNode(temp);
curr=curr.next;
}
return mer.next;
}
void add(ListNode head){
while(head!=null){
p.add(head.val);
head=head.next;
}
}
}
边栏推荐
猜你喜欢
batch_size of deep learning foundation
批量--09---批量读文件入表
Qt编写物联网管理平台49-设备模拟工具
ADSP21489仿真:Failed to set breakpoint: Can‘t set breakpoints in the current state: Running
Research Notes (8) Deep Learning and Its Application in WiFi Human Perception (Part 2)
可视水印的实现——1使用加法实现(add,+)
8月1日“海豹数藏”将全网首发民族英雄林则徐《四行行书》数字藏品!
ScholarOne Manuscripts提交期刊LaTeX文件,无法成功转换PDF!
MapFi paper structure organization
多数据中心操作和检测并发写入
随机推荐
什么是接触电流怎么测?
Win8.1下QT4.8集成开发环境的搭建
Research Notes (6) Indoor Path Planning Method Based on Environment Perception
MySQL read-write separation mysql-proxy deployment
最后写入胜利(丢弃并发写入)
复制延迟案例(3)-单调读
SCI期刊最权威的信息查询步骤!
batch_size of deep learning foundation
迭代器与生成器
ES6中变量的使用及结构赋值
多主复制下处理写冲突(4)-多主复制拓扑
MapFi论文架构整理
Location、navigator和History对象
VisibleDeprecationWarning: Creating an ndarray from ragged nested sequences (which is a list-or-tupl
Kubernetes中Pod对象学习笔记
高等数学(第七版)同济大学 总习题三(后10题) 个人解答
其他语法和模块的导出导入
数据可视化之百变柱状图
Centos7下使用systemd管理redis服务启动
吴恩达机器学习系列课程笔记——第六章:逻辑回归(Logistic Regression)