当前位置:网站首页>Force deduction 23 questions, merging K ascending linked lists
Force deduction 23 questions, merging K ascending linked lists
2022-06-11 18:26:00 【Yingtai night snow】
Power button 23 topic , Merge K An ascending list
Title Description
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 .
I/o sample
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
Input :lists = []
Output :[]
Input :lists = [[]]
Output :[]
Solution 1 ,( Order merge )
// Merge k An ascending list
ListNode *mergeKLists(vector<ListNode*>&lists)
{
ListNode* resList;
int length=lists.size();
if(length==0)
{
return resList=nullptr;
}
else if(length==1)
{
resList=lists[0];
return resList;
}
resList=lists[0];
// Set flag bit
for(int i=1;i<length;i++)
{
resList=mergeList(resList,lists[i]);
}
// resList=mergeList(lists[0],lists[1]);
// cout<<" After the merger list Elements : ";
// printList(resList->next);
return resList;
}
ListNode *mergeList(ListNode *list1,ListNode *list2)
{
if(list1==nullptr)
{
return list2;
}
else if(list2==nullptr)
{
return list1;
}
else if(list1->val<list2->val)
{
list1->next=mergeList(list1->next,list2);
return list1;
}
else
{
list2->next=mergeList(list1,list2->next);
return list2;
}
}
use leetcode23 Method in question , First, recursively merge the two linked lists , Then it is extended to three , Consolidation of four or more linked lists .
Solution 2 : Using divide and conquer algorithm
ListNode *mergeKList2(vector<ListNode*>&lists)
{
int length=lists.size();
ListNode* resList;
if(length==0)
{
return resList=nullptr;
}
else if(length==1)
{
resList=lists[0];
return resList;
}
int interval=1;
while(interval<length)
{
for(int i=0;i<length-interval;i=i+2*interval)
{
lists[i]=mergeList(lists[i],lists[i+interval]);
}
interval*=2;
}
return lists[0];
}
Divide and conquer algorithm is a process of pairwise matching step by step compared with sequential merging .
边栏推荐
猜你喜欢

合并多棵二叉搜索树

NR LDPC 打孔-punctured
![[Golang]力扣Leetcode - 349. 两个数组的交集(哈希表)](/img/92/03de54c9f08eae5bc920b04d2b8493.png)
[Golang]力扣Leetcode - 349. 两个数组的交集(哈希表)
![[c language] output the students with the highest scores with a structure. There can be multiple highest scores](/img/4e/836a8f717a2d9bf5f999a934ff4c91.png)
[c language] output the students with the highest scores with a structure. There can be multiple highest scores

神经网络与深度学习-2- 机器学习简单示例-PyTorch

Reading summary of nacos2.x source code

信号的处理与捕捉

Understanding of distributed transactions

使用Visdom对损失函数进行监控

TR-069 protocol introduction
随机推荐
labelme进行图片数据标注
求字符串中最大的 3 位相同数字
Common interview questions of network and concurrent programming
了解一下random库·1
5分钟了解攻防演练中的红蓝紫
Reading summary of nacos2.x source code
5 minutes to understand the red, blue and purple in the attack and defense drill
【无标题】
牛客刷题——合法括号序列判断
MySQL/Redis 常见面试题汇总
RadioGroup动态添加RadioButton
SQL error injection 1
Feign shares login information for request
* Jetpack 笔记 使用DataBinding
在同花顺上面开股票账户好不好,安不安全?
Labelme for image data annotation
Ubuntu installs PSQL and runs related commands
LDAP 目录服务器的现代化应用
关于keil中,while循环条件不成立却无法跳出的问题
力扣33题,搜索旋转排序数组