当前位置: 首页 > 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.gsyq.cn/news/55487.html

相关文章:

  • Windows 11 上安装 JDK
  • cacti 监控 linux
  • 用了会Windows 10
  • 2025 年 11 月牛奶分析仪厂家推荐排行榜,实验室/进口/全自动牛奶分析仪,乳品厂/奶农/牧场用牛奶分析仪,德国盖博/FUNKE GERBER/LUM及美国PerkinElmer品牌精选
  • 哈希表封装myunordered_map以及set - 详解
  • LangGraph1.0智能体本地开发调测搭建
  • 朝阳区婚姻律师事务所推荐:婚姻家事法律服务机构参考
  • 北京婚姻家庭法律事务所服务及专业机构参考
  • 北京处理家暴案件厉害的律师有哪些?行业实务参考
  • 北京离婚官司最厉害的律师有哪些?婚姻纠纷解决团队参考
  • 电商业务
  • 北京家事律师事务所有哪些?本地专业机构信息整理
  • 磁悬浮轴承非线性控制的挑战与难点剖析 - 实践
  • 绩效管理千万要抓好这2大关键,3个前提!
  • 有智能功能的家用咖啡机品牌推荐
  • 完整教程:WPF 核心概念笔记(补充示例)
  • 2025 最新铁芯源头厂家权威推荐榜:精准工艺加持,全场景适配品牌实力甄选环形铁芯/互感器铁芯公司推荐
  • Swift报错“EXC_BAD_ACCESS“?内存管理的ARC原理深度解析 - 详解
  • 2025年深圳CE标准机构权威推荐榜单:CE认证标准/CE检测认证/CE检测报告源头机构精选
  • 2025 最新加工厂家推荐!车铣复合 / 精密零件 / CNC 数控等加工服务品牌排行榜:权威甄选优质合作商高精度走心机 / 不锈钢铝合金黄铜非标零件加工推荐
  • wps 取消自动编号
  • NocoBase 本周更新汇总:新增图表配置的 Al 员工
  • 虚拟机上redhat7.2安装oracle 11g rac 集群
  • 2025年深圳会议室麦克风公司权威推荐榜单:无线会议话筒/桌面会议话筒/无线手拉手会议话筒源头公司精选
  • 2025 最新推荐!塑料板材设备厂家排行榜单:覆盖 PP/ABS/PE/PC 全系列生产线ABS 塑料板材设备/PE 塑料板材设备/PC 塑料板材设备公司推荐
  • oeasy玩py106 列表_删除_del_索引元素_切片
  • 2025年 11月 上海网站建设与小程序一体化搭建方案 甄选推荐
  • 2025云南冻品批发供应商最新TOP5权威推荐 餐饮店优质冻品源头厂家、烧烤食材品牌选购指南
  • 低功耗抗干扰液晶驱动工控仪表段码驱动显示IC VK2C21BA LCD驱动原厂
  • 【完整版】Grok 4.1全面官方解析:功能详解+API调用+在线使用入口