当前位置:网站首页>【Hot100】23. Merge K ascending linked lists
【Hot100】23. Merge K ascending linked lists
2022-07-02 19:57:00 【Wang Liuliu's it daily】
23. Merge K An ascending list
Difficult questions
Here's an array of linked lists , Each list has been listed in ascending order .
Please merge all the linked lists into one ascending list , Return the merged linked list .
Ideas : Merge two linked lists one by one
First of all, review 【Hot100】21. Merge two ordered lists
The following are posted 「merge2Lists」 Of 「 recursive 」 and 「 iteration 」 Two implementations :
- recursive
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;
}
}
}
- iteration
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;
}
- Merge one by one
class Solution {
public ListNode mergeKLists(ListNode[] lists) {
ListNode res = null;
for (ListNode list: lists) {
res = merge2Lists(res, list);
}
return res;
}
}
Pair by pair 1 To optimize :
- A merger of two - iteration
/** * 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;
}
}
- A merger of two - recursive
/** * 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);
}
// Merge functions
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);
}
// Merge two ascending linked lists
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;
}
// Move and merge the next node of the linked list
cur = cur.next;
}
// If linked list 1 It's empty , Then merge the linked list and put the linked list 2 The rest is connected at the back , Otherwise, connect the linked list 1 node
if(l1 == null){
cur.next = l2;
}else{
cur.next = l1;
}
//cur.next = l1 == null? l2 : l1;
return dummyHead.next;
}
}
边栏推荐
- Génération automatique de fichiers d'annotation d'images vgg
- 451 implementation of memcpy, memmove and memset
- 数据库模式笔记 --- 如何在开发中选择合适的数据库+关系型数据库是谁发明的?
- 自動生成VGG圖像注釋文件
- esp32c3 crash分析
- RPD product: super power squad nanny strategy
- AcWing 903. Expensive bride price solution (the shortest path - building map, Dijkstra)
- AcWing 1125. Cattle travel problem solution (shortest path, diameter)
- 分享几个图床网址,便于大家分享图片
- 解决方案:VS2017 无法打开源文件 stdio.h main.h 等头文件[通俗易懂]
猜你喜欢
Development skills of rxjs observable custom operator
Build a master-slave mode cluster redis
burp 安装 license key not recognized
【NLP】一文详解生成式文本摘要经典论文Pointer-Generator
Istio部署:快速上手微服务,
CRM Customer Relationship Management System
有时候只查询一行语句,执行也慢
Py's interpret: a detailed introduction to interpret, installation, and case application
RPD出品:Superpower Squad 保姆级攻略
八年测开经验,面试28K公司后,吐血整理出高频面试题和答案
随机推荐
Google Earth engine (GEE) - Landsat 9 image full band image download (Beijing as an example)
励志!大凉山小伙全奖直博!论文致谢看哭网友
Embedded (PLD) series, epf10k50rc240-3n programmable logic device
Kt148a voice chip instructions, hardware, protocols, common problems, and reference codes
Py's interpret: a detailed introduction to interpret, installation, and case application
KT148A语音芯片ic的硬件设计注意事项
勵志!大凉山小夥全獎直博!論文致謝看哭網友
From 20s to 500ms, I used these three methods
rxjs Observable 自定义 Operator 的开发技巧
Detailed explanation of VBScript (I)
B-end e-commerce - reverse order process
编写完10万行代码,我发了篇长文吐槽Rust
AcWing 1134. Shortest circuit counting problem solution (shortest circuit)
Share several map bed websites for everyone to share pictures
Exemple complet d'enregistrement du modèle pytoch + enregistrement du modèle pytoch seuls les paramètres d'entraînement sont - ils enregistrés? Oui (+ Solution)
Google Earth Engine(GEE)——Landsat 9影像全波段影像下载(北京市为例)
Automatically generate VGg image annotation file
Overview of browser caching mechanism
CheckListBox control usage summary
Set up sentinel mode. Reids and redis leave the sentinel cluster from the node