当前位置:网站首页>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;
}
}
}
边栏推荐
- 2020 intermediate financial management (escort class)
- EGO Planner代码解析bspline_optimizer部分(3)
- SSM整合-前后台协议联调(列表功能、添加功能、添加功能状态处理、修改功能、删除功能)
- Record: install MySQL on ubuntu18.04
- Ego planner code parsing Bspline_ Optimizer section (3)
- DriveSeg:动态驾驶场景分割数据集
- The way to treat feelings
- shell 脚本中关于用户输入参数的处理
- SSM integration - joint debugging of front and rear protocols (list function, add function, add function status processing, modify function, delete function)
- 东数西算拉动千亿产业,敢啃“硬骨头”的存储厂商才更有机会
猜你喜欢

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
![Free hand account sharing in September - [cream Nebula]](/img/4f/fec31778a56886585e35be87885452.jpg)
Free hand account sharing in September - [cream Nebula]

Think of new ways

Day_ 18 IO stream system

我们做了一个智能零售结算平台
![leetcode:11. Container with the most water [double pointer + greed + remove the shortest board]](/img/d4/cbbaec40119be6cb5594899e348261.png)
leetcode:11. Container with the most water [double pointer + greed + remove the shortest board]

Free year-end report summary template Welfare Collection

If the warehouse management communication is not in place, what problems will occur?

【数学建模】基于matlab船舶三自由度MMG模型【含Matlab源码 1925期】
随机推荐
Yolov3 network model building
How to build an efficient information warehouse
Random numbers in a long range, is that right- Random number in long range, is this the way?
Su embedded training - Day10
Leetcode: 11. Récipient contenant le plus d'eau [double pointeur + cupidité + enlèvement de la plaque la plus courte]
OSPF - detailed explanation of stub area and full stub area
Failed to start component [StandardEngine[Catalina]. StandardHost[localhost]. StandardContext
EGO Planner代码解析bspline_optimizer部分(2)
[optics] dielectric constant calculation based on MATLAB [including Matlab source code 1926]
leetcode:11. Container with the most water [double pointer + greed + remove the shortest board]
SQL custom collation
I didn't cancel
【学术相关】顶级论文创新点怎么找?中国高校首次获CVPR最佳学生论文奖有感...
[leetcode] [SQL] notes
变化是永恒的主题
Day-27 database
[mathematical modeling] ship three degree of freedom MMG model based on MATLAB [including Matlab source code 1925]
【光学】基于matlab涡旋光产生【含Matlab源码 1927期】
Dynamic planning -- expansion topics
math_ Taylor formula