当前位置:网站首页>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;
}
}
}
边栏推荐
- High concurrency Architecture - separate databases and tables
- High concurrency Architecture - read write separation
- 硬盘监控和分析工具:Smartctl
- Flask generates swagger documents
- Briefly describe the quantitative analysis system of services
- 2020 intermediate financial management (escort class)
- my. INI file not found
- __ Weak and__ The difference between blocks
- [optics] dielectric constant calculation based on MATLAB [including Matlab source code 1926]
- Using the visualization results, click to appear the corresponding sentence
猜你喜欢
【疾病识别】基于matlab GUI机器视觉肺癌检测系统【含Matlab源码 1922期】
Ctrip will implement a 3+2 work system in March, with 3 days on duty and 2 days at home every week
SQL: special update operation
Yolov3 network model building
leetcode:11. 盛最多水的容器【雙指針 + 貪心 + 去除最短板】
Think of new ways
[mathematical modeling] ship three degree of freedom MMG model based on MATLAB [including Matlab source code 1925]
平淡的生活里除了有扎破皮肤的刺,还有那些原本让你魂牵梦绕的诗与远方
Pytorch introduction to deep learning practice notes 13- advanced chapter of cyclic neural network - Classification
Using the visualization results, click to appear the corresponding sentence
随机推荐
FBI warning: some people use AI to disguise themselves as others for remote interview
What does a really excellent CTO look like in my eyes
Integrated easy to pay secondary domain name distribution system
[new year job hopping season] test the technical summary of interviewers' favorite questions (with video tutorials and interview questions)
High concurrency Architecture - separate databases and tables
Smart wax therapy machine based on STM32 and smart cloud
leetcode:11. Container with the most water [double pointer + greed + remove the shortest board]
235. 二叉搜索树的最近公共祖先【lca模板 + 找路径相同】
“google is not defined” when using Google Maps V3 in Firefox remotely
Le changement est un thème éternel
Which do MySQL and Oracle learn?
东数西算拉动千亿产业,敢啃“硬骨头”的存储厂商才更有机会
【Proteus仿真】用24C04与1602LCD设计的简易加密电子密码锁
SQL custom collation
235. The nearest common ancestor of the binary search tree [LCA template + same search path]
Failed to start component [StandardEngine[Catalina]. StandardHost[localhost]. StandardContext
Today I am filled with emotion
How to read the source code [debug and observe the source code]
[wallpaper] (commercially available) 70 wallpaper HD free
Help change the socket position of PCB part