【Hot100】LeetCode—23. 合并 K 个升序链表

目录

  • [1- 思路](#1- 思路)
  • [2- 实现](#2- 实现)
    • [⭐23. 合并 K 个升序链表------题解思路](#⭐23. 合并 K 个升序链表——题解思路)
  • [3- ACM 实现](#3- ACM 实现)


1- 思路

优先队列

  • 1- 提供的数据结构:ListNode[] lists
  • 2- 由于提供的数据结构已经是有序的,不能通过指针实现是因为不知道一共要定义多少个指针来遍历链表
  • 3- 所以引出优先队列解决,将链表的数据加入 优先队列,实现排序

实现思路

java 复制代码
PriorityQueue<ListNode> pq = new PriorityQueue<>(len,Comparator.comparingInt(a -> a.val));
  • 上述操作实际上实现了一个最小堆,每次弹出的是一个堆顶元素。
  • 如果定义的是升序优先队列,则其弹出 poll 的时候,始终是最小的元素先弹出``

2- 实现

⭐23. 合并 K 个升序链表------题解思路

java 复制代码
class Solution {
    public ListNode mergeKLists(ListNode[] lists) {
        ListNode dummyHead = new ListNode(-1);
        ListNode cur = dummyHead;
        // 定义长度
        int len = lists.length;
        if(len==0){
            return null;
        }


        // 1.优先队列
        PriorityQueue<ListNode> pq = new PriorityQueue<>(len,Comparator.comparingInt(a -> a.val));

        // 2.先加入头
        for(ListNode list:lists){
            if(list!=null){
                pq.add(list);
            }
        }

        // 3. 处理弹出逻辑
        while(!pq.isEmpty()){
            cur.next = pq.poll();
            cur = cur.next;
            if(cur.next!=null){
                pq.add(cur.next);
            }
        }
        return dummyHead.next;
    }
}

3- ACM 实现

java 复制代码
public class mergeKLists {
    public static class ListNode {
        int val;
        ListNode next;
        ListNode(int x) {
            val = x;
            next = null;
        }
    }

    public static ListNode mergKLists(ListNode[] lists){
        // 1. 求长度
        int len = lists.length;
        if(len==0){
            return null;
        }
        // 1. 定义 pq
        PriorityQueue<ListNode> pq = new PriorityQueue<>(len, Comparator.comparingInt(a -> a.val));

        // 2.处理头
        for(ListNode list:lists){
            if(list!=null){
                pq.add(list);
            }
        }
        ListNode dummyHead = new ListNode(-1);
        ListNode cur = dummyHead;
        // 3. 处理pq
        while(!pq.isEmpty()){
            cur.next = pq.poll();
            cur = cur.next;
            if(cur.next!=null){
                pq.add(cur.next);
            }
        }
        return dummyHead.next;
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("数组长度");
        int k = scanner.nextInt();
        ListNode[] lists = new ListNode[k];
        for (int i = 0; i < k; i++) {
            System.out.println("输入链表长度");
            int n = scanner.nextInt();
            if (n == 0) {
                lists[i] = null;
            } else {
                lists[i] = new ListNode(scanner.nextInt());
                ListNode current = lists[i];
                for (int j = 1; j < n; j++) {
                    current.next = new ListNode(scanner.nextInt());
                    current = current.next;
                }
            }
        }
        ListNode forRes = mergKLists(lists);
        while(forRes!=null){
            System.out.print(forRes.val+" ");
            forRes = forRes.next;
        }
    }
}
相关推荐
ゞ 正在缓冲99%…7 小时前
leetcode2826.将三个组排序
算法·leetcode·动态规划
qq_401700418 小时前
matlab学习
学习·算法·matlab
budingxiaomoli9 小时前
算法--滑动窗口(一)
数据结构·算法
王哈哈^_^10 小时前
【数据集】【YOLO】【目标检测】农作物病害数据集 11498 张,病害检测,YOLOv8农作物病虫害识别系统实战训推教程。
人工智能·深度学习·算法·yolo·目标检测·计算机视觉·1024程序员节
xier_ran10 小时前
邻接矩阵的 k 次幂意味着什么?从图论到路径计数的直观解释
算法·图论
B站_计算机毕业设计之家11 小时前
预测算法:股票数据分析预测系统 股票预测 股价预测 Arima预测算法(时间序列预测算法) Flask 框架 大数据(源码)✅
python·算法·机器学习·数据分析·flask·股票·预测
想唱rap12 小时前
C++ list 类的使用
c语言·开发语言·数据结构·c++·笔记·算法·list
l1t12 小时前
利用DuckDB SQL求解集合数学题
数据库·sql·算法·集合·duckdb
yuyanjingtao12 小时前
CCF-GESP 等级考试 2024年9月认证C++四级真题解析
c++·算法·青少年编程·gesp·csp-j/s
微笑尅乐12 小时前
洗牌算法讲解——力扣384.打乱数组
算法·leetcode·职场和发展