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

day17 leetcode-hot100-34(链表13)

23. 合并 K 个升序链表 - 力扣(LeetCode)

1.数组排序

思路

(1)将全部的节点存储到数组中

(2)对数组进行排序

(3)最后创建一个全新的链表

具体代码
/*** 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) {int len=0;for(int i=0;i<lists.length;i++){ListNode p = lists[i];while(p!=null){len++;p=p.next;}}int[] arr = new int[len];int k=0;for(int i=0;i<lists.length;i++){ListNode p = lists[i];while(p!=null){arr[k++]=p.val;p=p.next;}}Arrays.sort(arr);ListNode head = new ListNode();ListNode ans = head;for(int i=0;i<len;i++){ListNode newn = new ListNode();newn.val=arr[i];head.next=newn;head=newn;}return ans.next;}
}

2.两两比较合成链表

思路

两两合并,也就是for循环,每次两个链表进行合并,最后输出结果。

具体步骤:

(1)判断链表是否为空,不是空则p=lists[0]

(2)将p与lists中下一个列表合并,采用之前写过的两两合并方法。

(3)每次结束后后需要将p归为到原始节点,重新与下一个链表合并。

具体代码
/*** 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 p = lists[0];  for(int i=1 ; i<lists.length;i++){ListNode con = new ListNode();ListNode init = con;ListNode np = lists[i];while(p != null && np != null){if(p.val<=np.val){con.next=p;con=con.next;p=p.next;}else{con.next=np;con=con.next;np=np.next;}}if(p==null){con.next=np;}else{con.next=p;}p=init.next;}return p;}
}

3.优先队列

思路

将链表放入优先队列中(小顶堆),每次循环都去最小的加入新链表,直到队列为空。

具体步骤:
(1)构造优先队列

(2)将各个链表的首节点放入队列

(3)将最小的节点加入链表,然后该节点进入下一个位置,如果不是空的,则加入队列。

具体代码
/*** 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) {PriorityQueue<ListNode> pq = new PriorityQueue<>((o1,o2)->{return o1.val-o2.val;});for(ListNode node:lists){if(node!=null){pq.offer(node);}}ListNode ans = new ListNode();ListNode cur = ans;while(!pq.isEmpty()){ListNode s=pq.poll();cur.next=s;cur=cur.next;if(s.next!=null){s=s.next;pq.offer(s);}}return ans.next;}
}

http://www.xdnf.cn/news/10728.html

相关文章:

  • Oracle授权操作
  • P12592题解
  • 图论刷题1
  • PostgreSQL pgrowlocks 扩展
  • C++语法架构解说
  • day44 python 训练CNN网络并使用Grad-CAM可视化
  • 如何区分虚拟货币诈骗与经营失败?
  • selenium-自动更新谷歌浏览器驱动
  • PostgreSQL不同的等级认证体系
  • 浏览器网站禁止黏贴,但是要交作业怎么快速黏贴
  • 从多巴胺的诱惑到内啡肽的力量 | 个体成长代际教育的成瘾困局与破局之道
  • (九)学生写作画像可视化
  • 【数据分析】第三章 numpy(2)
  • cpper 转 Golang
  • 爬虫的几种方式(使用什么技术来进行一个爬取数据)
  • Github 2025-06-02 开源项目周报 Top11
  • QT之头像剪裁效果实现
  • 排序算法——详解
  • pikachu靶场通关笔记10 XSS关卡06-XSS之盲打
  • python打卡day43@浙大疏锦行
  • 定制开发开源AI智能名片驱动下的海报工厂S2B2C商城小程序运营策略——基于社群口碑传播与子市场细分的实证研究
  • 【Linux】Git原理与使用
  • MCP协议学习
  • 《Effective Python》第六章 推导式和生成器——将迭代器作为参数传递给生成器,而不是调用 send 方法
  • 【兽医处方专用软件】佳易王兽医电子处方软件:高效智能的宠物诊疗管理方案
  • 腾讯云 Python3.12.8 通过yum安装 并设置为默认版本
  • [android]MT6835 Android 指令启动MT6631 wifi操作说明
  • Android第十二次面试GetX库渲染机制
  • SpringBoot-Thymeleaf
  • React 18 生命周期详解与并发模式下的变化