当前位置:网站首页>【Hot100】23. 合并K个升序链表
【Hot100】23. 合并K个升序链表
2022-07-02 18:54:00 【王六六的IT日常】
23. 合并K个升序链表
困难题
给你一个链表数组,每个链表都已经按升序排列。
请你将所有链表合并到一个升序链表中,返回合并后的链表。
思路:逐一合并两条链表
首先复习一下【Hot100】21. 合并两个有序链表
下面分别贴出「merge2Lists」的「递归」 和 「迭代」两种实现 :
- 递归
class Solution {
public ListNode mergeTwoLists(ListNode list1, ListNode list2) {
if (list1 == null) {
return list2;
}
else if (list2 == null) {
return list1;
}
else if (list1.val < list2.val) {
list1.next = mergeTwoLists(list1.next, list2);
return list1;
}
else {
list2.next = mergeTwoLists(list1, list2.next);
return list2;
}
}
}
- 迭代
private ListNode merge2Lists(ListNode l1, ListNode l2) {
ListNode dummyHead = new ListNode(0);
ListNode tail = dummyHead;
while (l1 != null && l2 != null) {
if (l1.val < l2.val) {
tail.next = l1;
l1 = l1.next;
} else {
tail.next = l2;
l2 = l2.next;
}
tail = tail.next;
}
tail.next = l1 == null? l2: l1;
return dummyHead.next;
}
- 逐一合并
class Solution {
public ListNode mergeKLists(ListNode[] lists) {
ListNode res = null;
for (ListNode list: lists) {
res = merge2Lists(res, list);
}
return res;
}
}
两两合并对 1 进行优化:
- 两两合并 - 迭代
/** * 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;
}
int k = lists.length;
while (k > 1) {
int idx = 0;
for (int i = 0; i < k; i += 2) {
if (i == k - 1) {
lists[idx++] = lists[i];
} else {
lists[idx++] = merge2Lists(lists[i], lists[i + 1]);
}
}
k = idx;
}
return lists[0];
}
private ListNode merge2Lists(ListNode l1, ListNode l2) {
ListNode dummyHead = new ListNode(0);
ListNode tail = dummyHead;
while (l1 != null && l2 != null) {
if (l1.val < l2.val) {
tail.next = l1;
l1 = l1.next;
} else {
tail.next = l2;
l2 = l2.next;
}
tail = tail.next;
}
tail.next = l1 == null? l2: l1;
return dummyHead.next;
}
}
- 两两合并 - 递归
/** * 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;
}
return merge(lists, 0, lists.length - 1);
}
private ListNode merge(ListNode[] lists, int lo, int hi) {
if (lo == hi) {
return lists[lo];
}
int mid = lo + (hi - lo) / 2;
ListNode l1 = merge(lists, lo, mid);
ListNode l2 = merge(lists, mid + 1, hi);
return mergeTwoLists(l1, l2);
}
public ListNode mergeTwoLists(ListNode list1, ListNode list2) {
if (list1 == null) {
return list2;
}
else if (list2 == null) {
return list1;
}
else if (list1.val < list2.val) {
list1.next = mergeTwoLists(list1.next, list2);
return list1;
}
else {
list2.next = mergeTwoLists(list1, list2.next);
return list2;
}
}
}
class Solution {
public ListNode mergeKLists(ListNode[] lists) {
if (lists == null || lists.length == 0) return null;
return merge(lists, 0, lists.length - 1);
}
//合并函数
private ListNode merge(ListNode[] lists, int left, int right) {
if (left == right) return lists[left];
int mid = left + (right - left) / 2;
ListNode l1 = merge(lists, left, mid);
ListNode l2 = merge(lists, mid + 1, right);
return mergeTwoLists(l1, l2);
}
//合并两个升序链表
private ListNode mergeTwoLists(ListNode l1, ListNode l2) {
ListNode dummyHead = new ListNode(0);
ListNode cur = dummyHead;
while (l1 != null && l2 != null) {
if (l1.val < l2.val) {
cur.next = l1;
l1 = l1.next;
} else {
cur.next = l2;
l2 = l2.next;
}
//移动合并链表的下一个节点
cur = cur.next;
}
//如果链表1为空了,则合并链表把链表2剩下的接在后面,否则接链表1节点
if(l1 == null){
cur.next = l2;
}else{
cur.next = l1;
}
//cur.next = l1 == null? l2 : l1;
return dummyHead.next;
}
}
边栏推荐
- 数据湖(十二):Spark3.1.2与Iceberg0.12.1整合
- AcWing 1131. 拯救大兵瑞恩 题解(最短路)
- 基于SSM实现网上购物商城系统
- Istio deployment: quickly start microservices,
- Educational codeforces round 129 (rated for Div. 2) supplementary problem solution
- GCC: Graph Contrastive Coding for Graph Neural NetworkPre-Training
- AcWing 1134. Shortest circuit counting problem solution (shortest circuit)
- Dictionaries
- API文档工具knife4j使用详解
- Design and implementation of ks004 based on SSH address book system
猜你喜欢

Educational codeforces round 129 (rated for Div. 2) supplementary problem solution

Notes on hardware design of kt148a voice chip IC

GCC: Graph Contrastive Coding for Graph Neural NetworkPre-Training

Motivation! Big Liangshan boy a remporté le prix Zhibo! Un article de remerciement pour les internautes qui pleurent

c语言链表--待补充

rxjs Observable 自定义 Operator 的开发技巧

SQLite 3.39.0 发布,支持右外连接和全外连接
![[NLP] a detailed generative text Abstract classic paper pointer generator](/img/d8/a367c26b51d9dbaf53bf4fe2a13917.png)
[NLP] a detailed generative text Abstract classic paper pointer generator

Conscience summary! Jupyter notebook from Xiaobai to master, the nanny tutorial is coming!

KT148A语音芯片ic的硬件设计注意事项
随机推荐
Reading notes of "the way to clean structure" (Part 2)
Cuckoo filter
One side is volume, the other side is layoff. There are a lot of layoffs in byte commercialization department. What do you think of this wave?
AcWing 1126. Minimum cost solution (shortest path Dijkstra)
Workplace four quadrant rule: time management four quadrant and workplace communication four quadrant "suggestions collection"
RPD product: super power squad nanny strategy
453-atoi函数的实现
c语言里怎么设立优先级,细说C语言优先级
How to set priorities in C language? Elaborate on C language priorities
解决方案:VS2017 无法打开源文件 stdio.h main.h 等头文件[通俗易懂]
Motivation! Big Liangshan boy a remporté le prix Zhibo! Un article de remerciement pour les internautes qui pleurent
[Chongqing Guangdong education] reference materials for labor education of college students in Nanjing University
AcWing 1131. 拯救大兵瑞恩 题解(最短路)
Use cheat engine to modify money, life and stars in Kingdom rush
有时候只查询一行语句,执行也慢
中缀表达式转换为后缀表达式(C语言代码+详解)
良心总结!Jupyter Notebook 从小白到高手,保姆教程来了!
pxe装机「建议收藏」
开始练习书法
AcWing 383. Sightseeing problem solution (shortest circuit)