当前位置:网站首页>24. Merge K ascending linked lists
24. Merge K ascending linked lists
2022-07-24 12:46:00 【Little happy】
23. Merge K An ascending list
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 .
Example 1:
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
Example 2:
Input :lists = []
Output :[]
Example 3:
Input :lists = [[]]
Output :[]
Divide and conquer algorithm
1: Divide and conquer , First the K The linked list is divided into two parts , Merge the two parts first , The linked list returned after merging these two parts .
/** * 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 ans = null;
for(int i=0;i<lists.length;i++){
ans = merge(ans,lists[i]);
}
return ans;
}
public ListNode merge(ListNode a,ListNode b){
if(a==null||b==null){
return a!=null?a:b;
}
ListNode head = new ListNode(0);
ListNode tail = head,p = a,q =b;
while(p!=null&&q!=null){
if(p.val>=q.val){
ListNode c = new ListNode(q.val);
tail.next = c;
tail = c;
q = q.next;
}
else{
ListNode c = new ListNode(p.val);
tail.next = c;
tail = c;
p = p.next;
}
}
if(p!=null) tail.next = p;
if(q!=null) tail.next = q;
return head.next;
}
}
边栏推荐
- About packaging objects
- SSM在线校园相册管理平台
- Error: [synth 8-439] module 'xxx' not found not found error solution
- 【C语言】详细的文件操作相关知识
- Ah, I am uncle card space
- [leetcode]- linked list-3
- Teach you how to use power Bi to realize four kinds of visual charts
- How to render millions of 2D objects smoothly with webgpu?
- 高速成长的背后,华为云乌兰察布数据中心的绿色之道
- Is there a free and commercially available website for US media video clips?
猜你喜欢

6-16漏洞利用-rlogin最高权限登陆

Cluster construction based on kubernetes v1.24.0 (I)

Custom scroll bar
Say no to blackmail virus, it's time to reshape data protection strategy

突破内存墙能带来什么?看火山引擎智能推荐服务节支增效实战

Seckill implementation diagram

SSM在线校园相册管理平台

The price of domestic flagship mobile phones is nearly 6000, but they can't even beat iphone12. It's clear who users choose

【Rust】引用和借用,字符串切片 (slice) 类型 (&str)——Rust语言基础12

深圳地铁12号线第二批工程验收通过 预计7月28日试运行
随机推荐
Is there a free and commercially available website for US media video clips?
The second batch of projects of Shenzhen Metro Line 12 passed the acceptance and is expected to be put into trial operation on July 28
Reserved instances & Savings Plans
Reserved instances & Savings Plans
Ah, I am uncle card space
深圳地铁12号线第二批工程验收通过 预计7月28日试运行
SSH服务突然连接不了案例总结
iSCSI新应用,以及NFS的存储服务分离
月薪 3万人民币是一种怎样的体验?做自媒体可以达到这种水平吗
2022.07.15 暑假集训 个人排位赛(十)
Buckle practice - sum of 34 combinations
Solutions to problems in IE6 browser
regular
Buckle practice - 24 remove repeated letters
国产旗舰手机定价近六千,却连iPhone12都打不过,用户选谁很明确
生信识图 之 点图基础
[function test] test of the project - login and post function
以Chef和Ansible为例快速入门服务器配置
猿人学第五题
ERROR: [Synth 8-439] module ‘xxx‘ not found not found 错误解决办法