当前位置:网站首页>Merge K ascending linked lists
Merge K ascending linked lists
2022-07-03 19:11:00 【Star_.】
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 .
Input :
lists = [[1,4,5],[1,3,4],[2,6]]
Output :[1,1,2,3,4,4,5,6]
explain : The linked list array is as follows :
[ 1->4->5, 1->3->4, 2->6 ] Combine them into an ordered list to get .
1->1->2->3->4->4->5->6
Method 1 : Violence solution
I have done the ascending arrangement of two linked lists before , This time it is n individual , So you can use the function written before , Then go through it one by one, and then close it up , If there is too much data , The running time must exceed , Take a look 200ms.
/** * 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) {
ListNode listsum = null;
int len = lists.length;
for(int i=0;i<len;i++){
listsum = Merge(listsum,lists[i]);
}
return listsum;
}
public ListNode Merge(ListNode list1,ListNode list2){
if(list1==null)
return list2;
if(list2==null)
return list1;
if(list1.val<list2.val){
list1.next=Merge(list1.next,list2);
return list1;
}
else{
list2.next=Merge(list1,list2.next);
return list2;
}
}
}
Method 2 :
Divide and conquer :
The picture comes from Li Kou
/** * 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;
if(lists.length==1)
return lists[0];
int len = lists.length;
int mid = len/2;
ListNode []list1 = new ListNode[mid];
for(int i=0;i<mid;i++)
list1[i] =lists[i];
ListNode []list2 = new ListNode[len-mid];
for(int i=0,j=mid;j<len;j++,i++)
list2[i]=lists[j];
return Merge(mergeKLists(list1),mergeKLists(list2));
}
public ListNode Merge(ListNode list1,ListNode list2){
if(list1==null)
return list2;
if(list2==null)
return list1;
if(list1.val<list2.val){
list1.next=Merge(list1.next,list2);
return list1;
}
else{
list2.next=Merge(list1,list2.next);
return list2;
}
}
}
边栏推荐
- SSM整合-前后台协议联调(列表功能、添加功能、添加功能状态处理、修改功能、删除功能)
- HOW TO WRITE A DAILY LAB NOTE?
- Free hand account sharing in September - [cream Nebula]
- FBI警告:有人利用AI换脸冒充他人身份进行远程面试
- Which do MySQL and Oracle learn?
- [proteus simulation] a simple encrypted electronic password lock designed with 24C04 and 1602LCD
- 22.2.14 -- station B login with code -for circular list form - 'no attribute' - 'needs to be in path selenium screenshot deviation -crop clipping error -bytesio(), etc
- Smart wax therapy machine based on STM32 and smart cloud
- Compose LazyColumn 顶部添加控件
- Suffix derivation based on query object fields
猜你喜欢

FBI警告:有人利用AI换脸冒充他人身份进行远程面试

Does SQL always report foreign key errors when creating tables?

EGO Planner代码解析bspline_optimizer部分(2)

A green plug-in that allows you to stay focused, live and work hard

Valentine's Day - make an exclusive digital collection for your lover

【数学建模】基于matlab船舶三自由度MMG模型【含Matlab源码 1925期】
![235. The nearest common ancestor of the binary search tree [LCA template + same search path]](/img/f5/f2d244e7f19e9ddeebf070a1d06dce.png)
235. The nearest common ancestor of the binary search tree [LCA template + same search path]

Day18 - basis of interface testing

In addition to the prickles that pierce your skin, there are poems and distant places that originally haunt you in plain life

leetcode:11. 盛最多水的容器【雙指針 + 貪心 + 去除最短板】
随机推荐
Go home early today
Typescript configuration
235. Ancêtre public le plus proche de l'arbre de recherche binaire [modèle LCA + même chemin de recherche]
Which do MySQL and Oracle learn?
ActiveMQ的基础
The most valuable thing
Differential constrained SPFA
[new year job hopping season] test the technical summary of interviewers' favorite questions (with video tutorials and interview questions)
FBI warning: some people use AI to disguise themselves as others for remote interview
Change is the eternal theme
Compose LazyColumn 顶部添加控件
OSPF - detailed explanation of stub area and full stub area
硬盘监控和分析工具:Smartctl
记录在模拟器中运行flutter时报的错
HOW TO WRITE A DAILY LAB NOTE?
[optics] dielectric constant calculation based on MATLAB [including Matlab source code 1926]
The earliest record
东数西算拉动千亿产业,敢啃“硬骨头”的存储厂商才更有机会
The installation path cannot be selected when installing MySQL 8.0.23
Latex image rotates with title