当前位置:网站首页>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;
}
}
}
边栏推荐
- Work Measurement - 1
- Yolov3 network model building
- How can I avoid "div/0!" Errors in Google Docs spreadsheet- How do I avoid the '#DIV/0!' error in Google docs spreadsheet?
- EGO Planner代码解析bspline_optimizer部分(1)
- Pan for in-depth understanding of the attention mechanism in CV
- Driveseg: dynamic driving scene segmentation data set
- Free sharing | linefriends hand account inner page | horizontal grid | not for sale
- Getting started with JDBC
- The way to treat feelings
- 知其然,而知其所以然,JS 对象创建与继承【汇总梳理】
猜你喜欢
![Free hand account sharing in September - [cream Nebula]](/img/4f/fec31778a56886585e35be87885452.jpg)
Free hand account sharing in September - [cream Nebula]
![[academic related] how to find the innovation of top papers? Chinese universities won the CVPR Best Student Thesis Award for the first time](/img/06/5a37e2dca9711f8322b657581c3d75.png)
[academic related] how to find the innovation of top papers? Chinese universities won the CVPR Best Student Thesis Award for the first time

User identity used by startup script and login script in group policy

Day_ 18 IO stream system

Verilog HDL continuous assignment statement, process assignment statement, process continuous assignment statement

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

Flutter网络和数据存储框架搭建 -b1

Chisel tutorial - 06 Phased summary: implement an FIR filter (chisel implements 4-bit FIR filter and parameterized FIR filter)

Su embedded training - Day10

Valentine's Day - make an exclusive digital collection for your lover
随机推荐
SQL custom collation
Processing of user input parameters in shell script
[academic related] how to find the innovation of top papers? Chinese universities won the CVPR Best Student Thesis Award for the first time
leetcode:11. 盛最多水的容器【双指针 + 贪心 + 去除最短板】
Sqlalchemy - subquery in a where clause - Sqlalchemy - subquery in a where clause
SSM integration - joint debugging of front and rear protocols (list function, add function, add function status processing, modify function, delete function)
Record: writing MySQL commands
Php based campus lost and found platform (automatic matching push)
【光学】基于matlab涡旋光产生【含Matlab源码 1927期】
During MySQL installation, the download interface is empty, and the components to be downloaded are not displayed. MySQL installer 8.0.28.0 download interface is empty solution
【光学】基于matlab介电常数计算【含Matlab源码 1926期】
Why should we do feature normalization / standardization?
Recommend a simple browser tab
If the warehouse management communication is not in place, what problems will occur?
EGO Planner代码解析bspline_optimizer部分(1)
Briefly describe the quantitative analysis system of services
Ego planner code parsing Bspline_ Optimizer section (3)
leetcode:556. 下一个更大元素 III【模拟 + 尽可能少变更】
Analyse du Code du planificateur ego bspline Section Optimizer (1)
[proteus simulation] a simple encrypted electronic password lock designed with 24C04 and 1602LCD