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

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;}
}

相关文章:

  • 每日算法刷题计划Day20 6.2:leetcode二分答案3道题,用时1h20min
  • 从 LeetCode 到日志匹配:一行 Swift 实现规则识别
  • 力扣LeetBook数组和字符串--数组简介
  • 【软件测试】web自动化:Pycharm+Selenium+Firefox(一)
  • NodeJS全栈WEB3面试题——P8项目实战类问题(偏全栈)
  • 电脑wifi显示已禁用怎么点都无法启用
  • Java线程生命周期详解
  • GStreamer开发笔记(六):gstreamer基本概念:组件、箱柜、管道、衬垫、链接组件
  • 【存储基础】存储设备和服务器的关系和区别
  • 4、ubuntu系统 | 文本和目录操作函数
  • 设备驱动与文件系统:03 生磁盘的使用
  • Python 训练营打卡 Day 33-神经网络
  • SpringMVC的注解
  • Java设计模式之备忘录模式详解
  • 【小沐杂货铺】基于Three.JS构建IFC模型浏览器(WebGL、CAD、Revit、IFC)
  • 使用source ~/.bashrc修改环境变量之后,关闭服务器,在重启,环境变量还有吗?
  • Spark-TTS: AI语音合成的“变声大师“
  • 一步一步配置 Ubuntu Server 的 NodeJS 服务器详细实录——4. 配置服务器终端环境 zsh , oh my zsh, vim
  • isp中的 ISO代表什么意思
  • 实验一:PyTorch基本操作实验
  • 网站建设方案模板下载/公司网站设计的内容有哪些
  • 广东品牌网站建设多少钱/速推网
  • 怎样自己做刷赞网站/六年级下册数学优化设计答案
  • 有口碑的免费网站建设/李勇seo博客
  • 智慧企业解决方案/北京网站优化培训
  • 关于做摄影的网站/上海宝山网站制作