当前位置: 首页 > news >正文

合并 K 个升序链表-leetcode

题目描述

给你一个链表数组,每个链表都已经按升序排列。

请你将所有链表合并到一个升序链表中,返回合并后的链表。

示例 1:

输入:lists = [[1,4,5],[1,3,4],[2,6]]
输出:[1,1,2,3,4,4,5,6]
解释:链表数组如下:
[1->4->5,1->3->4,2->6
]
将它们合并到一个有序链表中得到。
1->1->2->3->4->4->5->6

示例 2:

输入:lists = []
输出:[]

示例 3:

输入:lists = [[]]
输出:[]

提示:

  • k == lists.length
  • 0 <= k <= 10^4
  • 0 <= lists[i].length <= 500
  • -10^4 <= lists[i][j] <= 10^4
  • lists[i]升序 排列
  • lists[i].length 的总和不超过 10^4

解法一

思路:

额外创建一个结果链表,从待选节点中选择出值最小的节点加入结果链表,同时更新待选节点。

代码:

/*** 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 == null || lists.length == 0) return null;ListNode dummy = new ListNode(0);ListNode cur = dummy;int stopFlag=0;for (ListNode list : lists) {if(list != null) stopFlag++;}if (stopFlag == 0) return null;int minIndex=0;while (stopFlag > 0) {minIndex=findMin(lists);int val=lists[minIndex].val;ListNode tmp=new ListNode(val);cur.next = tmp;cur=cur.next;lists[minIndex]=lists[minIndex].next;if(lists[minIndex]==null) stopFlag--;}return dummy.next;}public int findMin(ListNode[] head) {int minIndex = -1;for (int i = 0; i < head.length; i++) {if (head[i] != null) {if (minIndex == -1 || head[i].val < head[minIndex].val) {minIndex = i;}}}return minIndex;}
}

解法二

思路:

来自官方的解答。

用分治的方法进行合并。

image-20251120184331791

1

代码:

class Solution {public ListNode mergeKLists(ListNode[] lists) {return merge(lists, 0, lists.length - 1);}public ListNode merge(ListNode[] lists, int l, int r) {if (l == r) {return lists[l];}if (l > r) {return null;}int mid = (l + r) >> 1;return mergeTwoLists(merge(lists, l, mid), merge(lists, mid + 1, r));}public ListNode mergeTwoLists(ListNode a, ListNode b) {if (a == null || b == null) {return a != null ? a : b;}ListNode head = new ListNode(0);ListNode tail = head, aPtr = a, bPtr = b;while (aPtr != null && bPtr != null) {if (aPtr.val < bPtr.val) {tail.next = aPtr;aPtr = aPtr.next;} else {tail.next = bPtr;bPtr = bPtr.next;}tail = tail.next;}tail.next = (aPtr != null ? aPtr : bPtr);return head.next;}
}

解法三

思路:

  1. 每个链表的第一个节点作为候选加入一个小顶堆PriorityQueue)。
  2. 每次从堆里取出值最小的节点,接到结果链表末尾。
  3. 如果取出的节点还有下一个节点,则把它的下一个节点加入堆。
  4. 重复,直到堆为空

代码:

class Solution {class Status implements Comparable<Status> {int val;ListNode ptr;Status(int val, ListNode ptr) {this.val = val;this.ptr = ptr;}public int compareTo(Status status2) {return this.val - status2.val;}}PriorityQueue<Status> queue = new PriorityQueue<Status>();public ListNode mergeKLists(ListNode[] lists) {for (ListNode node: lists) {if (node != null) {queue.offer(new Status(node.val, node));}}ListNode head = new ListNode(0);ListNode tail = head;while (!queue.isEmpty()) {Status f = queue.poll();tail.next = f.ptr;tail = tail.next;if (f.ptr.next != null) {queue.offer(new Status(f.ptr.next.val, f.ptr.next));}}return head.next;}
}
http://www.jsqmd.com/news/45842/

相关文章:

  • 解码线程池
  • Windows 11 上安装 JDK
  • cacti 监控 linux
  • 2025年成都电线电缆采购标杆厂家最新推荐:成都鑫佰亿,电力电缆/高压电缆/中压电缆/低压电缆/铜芯电缆/铝芯电缆/树立电线电缆品质新标准
  • 2025年11月取暖器、电暖器十大品牌权威推荐:石墨烯取暖成主流,告别干燥严寒,解锁全屋舒适暖居体验
  • 社区伙伴活动推荐丨Global night第二场 连线日本!来玩!
  • 2025年成都电线电缆优质供应商推荐:铝合金电缆/家装电线/家用电线/铜芯电线/硬芯线/软电线/成都鑫佰亿,以品质与服务树立行业新标杆
  • 用了会Windows 10
  • Linux初级知识:sudo 提权
  • 2025 年 11 月牛奶分析仪厂家推荐排行榜,实验室/进口/全自动牛奶分析仪,乳品厂/奶农/牧场用牛奶分析仪,德国盖博/FUNKE GERBER/LUM及美国PerkinElmer品牌精选
  • 洛谷P1962 斐波那契数列 题解 矩阵快速幂
  • 2025最新青岛防水补漏服务TOP5口碑推荐:防水补漏/防水/补漏/堵漏/漏水检测服务全评测,守护建筑安全防线
  • 2025年11月汽车陪练十大品牌权威推荐:新手驾到领衔,科学陪驾助你从容上路,告别马路焦虑
  • 哈希表封装myunordered_map以及set - 详解
  • 2025 年语音 AI 趋势十大洞察丨Voice Agent 学习笔记
  • 斐波那契数列1-90
  • 使用RPA实现在线表格自动化编辑
  • 2025/11/22
  • Cursor部署markdown转Word的MCP工具教程
  • Cursor部署markdown转Word的MCP工具教程
  • 05 OpenCV实现图形的绘制
  • KingbaseES:MongoDB 国产化平替的优选实用的方案,从技巧适配到政务落地
  • centos修改主机名称
  • LangGraph1.0智能体本地开发调测搭建
  • 朝阳区婚姻律师事务所推荐:婚姻家事法律服务机构参考
  • 北京十佳婚姻家事律师事务所推荐及业务领域概述
  • 海淀区离婚律师事务所推荐:本地专业法律服务机构盘点
  • PLC编程培训哪家费用优惠?行业机构选择参考
  • 洛谷P3390 【模板】矩阵快速幂 题解 矩阵快速幂模板题
  • 防爆烘箱厂家哪家强?国内实力企业综合评析