当前位置:网站首页>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;
}
}
}
边栏推荐
- shell 脚本中关于用户输入参数的处理
- We have built an intelligent retail settlement platform
- php-fpm的max_chindren的一些误区
- PyTorch中在反向传播前为什么要手动将梯度清零?
- Random numbers in a long range, is that right- Random number in long range, is this the way?
- How to build an efficient information warehouse
- Record: MySQL changes the time zone
- EGO Planner代碼解析bspline_optimizer部分(1)
- Pan for in-depth understanding of the attention mechanism in CV
- 我们做了一个智能零售结算平台
猜你喜欢

Integrated easy to pay secondary domain name distribution system
![[free sharing] kotalog diary2022 plan electronic manual ledger](/img/ca/1ffbfcc16e3019261f70274a89c16f.jpg)
[free sharing] kotalog diary2022 plan electronic manual ledger
![[leetcode weekly race] game 300 - 6110 Number of incremental paths in the grid graph - difficult](/img/8d/0e515af6c17971ddf461e3f3b87c30.png)
[leetcode weekly race] game 300 - 6110 Number of incremental paths in the grid graph - difficult

What does a really excellent CTO look like in my eyes

Summary of composition materials for 2020 high-frequency examination center of educational resources

Php based campus lost and found platform (automatic matching push)

Driveseg: dynamic driving scene segmentation data set

【Proteus仿真】用24C04与1602LCD设计的简易加密电子密码锁

235. 二叉搜索樹的最近公共祖先【lca模板 + 找路徑相同】

OSPF - detailed explanation of stub area and full stub area
随机推荐
平淡的生活里除了有扎破皮肤的刺,还有那些原本让你魂牵梦绕的诗与远方
KINGS
leetcode:11. 盛最多水的容器【雙指針 + 貪心 + 去除最短板】
Day18 - basis of interface testing
[water quality prediction] water quality prediction based on MATLAB Fuzzy Neural Network [including Matlab source code 1923]
[leetcode weekly race] game 300 - 6110 Number of incremental paths in the grid graph - difficult
How to read the source code [debug and observe the source code]
In addition to the prickles that pierce your skin, there are poems and distant places that originally haunt you in plain life
“google is not defined” when using Google Maps V3 in Firefox remotely
Ego planner code parsing Bspline_ Optimizer section (2)
Processing of user input parameters in shell script
Flutter网络和数据存储框架搭建 -b1
Does SQL always report foreign key errors when creating tables?
Chisel tutorial - 06 Phased summary: implement an FIR filter (chisel implements 4-bit FIR filter and parameterized FIR filter)
leetcode:11. 盛最多水的容器【双指针 + 贪心 + 去除最短板】
Scrapy爬虫框架
【LeetCode】【SQL】刷题笔记
Record: writing MySQL commands
The way to treat feelings
PyTorch中在反向传播前为什么要手动将梯度清零?