【每日一题】【小根堆&边出队边入队后续节点&注意判空】23. 合并K个升序链表-211128/220213
给你一个链表数组,每个链表都已经按升序排列。
请你将所有链表合并到一个升序链表中,返回合并后的链表。

答案1(参数是数组):
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode() {}
* ListNode(int val) { this.val = val; }
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
* }
*/
class Solution {
public ListNode mergeKLists(ListNode[] lists) {
if(lists.length == 0) {
return null;
}
ListNode dummy = new ListNode(0);
ListNode cur = dummy;
//后大前小
PriorityQueue<ListNode> pq = new PriorityQueue<>((o1, o2) -> o1.val - o2.val);
for(ListNode list : lists) {
if(list == null) {
continue;
}
pq.add(list);
}
while(!pq.isEmpty()) {
ListNode next = pq.poll();
cur.next = next;
cur = cur.next;
if(next.next != null) {
pq.add(next.next);
}
}
return dummy.next;
}
}
答案2:(参数是ArrayList)
import java.util.*;
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) {
* val = x;
* next = null;
* }
* }
*/
public class Solution {
public ListNode mergeKLists(ArrayList<ListNode> lists) {
ListNode res = new ListNode(0);
ListNode cur = res;
PriorityQueue<ListNode> queue =
new PriorityQueue<>((o1, o2) -> o1.val - o2.val);
for(ListNode list : lists) {
if(list != null) {
queue.add(list);
}
}
while(!queue.isEmpty()) {
ListNode node = queue.poll();
if(node == null) {
continue;
}
cur.next = node;
cur = cur.next;
if(node.next != null) {
queue.offer(node.next);
}
}
return res.next;
}
}
自己方法:(超时)
import java.util.*;
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) {
* val = x;
* next = null;
* }
* }
*/
public class Solution {
public ListNode mergeKLists(ArrayList<ListNode> lists) {
//使用优先队列(小根堆)
PriorityQueue<ListNode> queue =
new PriorityQueue<>((o1, o2) -> o1.val - o2.val);
ListNode res = new ListNode(0);
ListNode cur = res;
Iterator<ListNode> iter = lists.iterator();
while(iter.hasNext()) {
ListNode list = iter.next();
while(list != null) {
queue.add(list);
list = list.next;
}
}
while(!queue.isEmpty()) {
cur.next = queue.poll();
cur = cur.next;
}
return res.next;
}
}
【每日一题】【小根堆&边出队边入队后续节点&注意判空】23. 合并K个升序链表-211128/220213的更多相关文章
- Leetcode题库——23.合并k个排序链表
@author: ZZQ @software: PyCharm @file: mergeKLists.py @time: 2018/10/12 19:55 说明:合并 k 个排序链表,返回合并后的排序 ...
- 代码题(14)— 合并有序链表、数组、合并K个排序链表
1.21. 合并两个有序链表 将两个有序链表合并为一个新的有序链表并返回.新链表是通过拼接给定的两个链表的所有节点组成的. 示例: 输入:1->2->4, 1->3->4 输出 ...
- leecode刷题(27)-- 合并k个排序链表
leecode刷题(27)-- 合并k个排序链表 合并k个排序链表 合并 k 个排序链表,返回合并后的排序链表.请分析和描述算法的复杂度. 示例: 输入: [ 1->4->5, 1-> ...
- leecode第二十三题(合并K个排序链表)
/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode ...
- 【LeetCode每天一题】 Merge k Sorted Lists(合并K个有序链表)
Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. E ...
- leetcode第23题:合并K个排序链表
首先我想到的是采用一般递归法,将K个链表合并化为(k-1)两个链表合并 class Solution: def mergeKLists(self, lists: List[ListNode]) -&g ...
- LeetCode 腾讯精选50题--合并K个排序链表
今天的题目稍微有点复杂了,因为是K个有序链表的合并,看到这道题后的大体思路是这样的: 1.首先先做到两个链表的合并,链表的合并我想到的是用递归操作, 2.其次是多个链表的合并,所以在第一步实现的基础上 ...
- 3,java数据结构和算法:约瑟夫环出队顺序, 单向环形链表的应用
什么是约瑟夫环? 就是数小孩游戏: 直接上代码: 要实现这个,只需要理清思路就好了 孩子节点: class Boy{ int no;//当前孩子的编码 Boy next; // 下一节点 public ...
- 【LeetCode】2020-03 每日一题
121. 买卖股票的最佳时机(简单) [分类]:模拟.思维 [题解]:可以用O(n)的复杂度完成,只需要在遍历的时候记录到当前位置为止买入股票的最小价格minn,再维护一个当前卖出股票价(a-minn ...
- 代码源 每日一题 分割 洛谷 P6033合并果子
题目链接:切割 - 题目 - Daimayuan Online Judge 数据加强版链接: [NOIP2004 提高组] 合并果子 加强版 - 洛谷 题目描述 有一个长度为 ∑ai 的木板,需要 ...
随机推荐
- docker垃圾处理
1 查找docker文件夹 find / -name docker 2 列举文件夹大小 du -h --time --max-depth=1 . df -h df -TH 3 Docker占用磁盘空间 ...
- 关于MongoDB副本集和分片集群有关用户和权限的说明分析
1.MongoDB副本集 可以先创建超管用户,然后再关闭服务,创建密钥文件,修改配置文件,启动服务,使用超管用户登录验证,然后创建普通用户 2.MongoDB分片集群 先关闭服务,创建密钥文件,修改配 ...
- 组合总和 II
组合总和 II 题目介绍 给定一个候选人编号的集合 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合. candidates ...
- MySQL学习(3)---MySQL常用命令
ps:此随笔基于mysql 5.7.*版本. 准备 net start mysql 启动MySQL服务 net stop mysql 关闭MySQL服务 mysql [-h<IP地址>] ...
- C++自学笔记 初始化列表 Initializer list
初始化p A(){ p = 0;cout<<"A::A()"<<endl;} 初始化列表 Initializer list A():p(0){ cout&l ...
- 洛谷P1216 [USACO1.5][IOI1994]数字三角形 Number Triangles (DP入门)
考虑逆推就行了. 1 #include<bits/stdc++.h> 2 using namespace std; 3 int n; 4 int a[1010][1010]; 5 int ...
- 备份 MySQL 的 shell 脚本(mysqldump版本) shell脚本
#!/bin/bash # 备份 MySQL 的 shell 脚本(mysqldump版本) # 定义变量 user(数据库用户名),passwd(数据库密码),date(备份的时间标签) # dbn ...
- JUC(3)
文章目录 1.集合类不安全 2.在高并发情况下arraylist()并不安全 3.高并发下set并不安全 3.测试map(高并发情况下出现问题) 1.集合类不安全 2.在高并发情况下arraylist ...
- JUI(6)线程池
文章目录 1.SynchronousQueue 2.线程池(重点) 2.1 使用单例 2.2.使用固定大小的线程 2.3.缓存线程池 2.4 七大参数 1.SynchronousQueue packa ...
- Java 8 Stream API 引入和使用
引入流 流是什么 流是Java API的新成员,它允许你以声明性的方式处理数据集合.可以看成遍历数据集的高级迭代.流可以透明地并行处理,无需编写多线程代码.我们先简单看一下使用流的好处.下面两段代码都 ...