当前位置:网站首页>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;
}
}
}
边栏推荐
猜你喜欢
随机推荐
Qt处理传输协议数据时QByteArray添加多字节的使用案例
Centos7下使用systemd管理redis服务启动
Transfer of UKlog.dat and QQ, WeChat files
节流阀和本地存储
2022华为软件精英挑战赛(初赛)-总结
[Win11] PowerShell cannot activate Conda virtual environment
数据复制系统设计(3)-配置新的从节点及故障切换
ADSP21489仿真:Failed to set breakpoint: Can‘t set breakpoints in the current state: Running
数据可视化之百变柱状图
Andrew Ng's Machine Learning Series Course Notes - Chapter 18: Application Example: Image Text Recognition (Application Example: Photo OCR)
热爱责任担当
ClickHouse的客户端命令行参数
Computer Basics
DOM系列之 click 延时解决方案
CaDDN paper reading of monocular 3D target detection
The CCF brush topic tour - the first topic
无主复制系统(3)-Quorum一致性的局限性
ScholarOne Manuscripts submits journal LaTeX file and cannot convert PDF successfully!
吴恩达机器学习系列课程笔记——第七章:正则化(Regularization)
复制延迟案例(2)-读己之写