当前位置:网站首页>Merge K sorted linked lists
Merge K sorted linked lists
2022-07-03 01:01:00 【Schuyler Hu】
problem

Ideas
Because the code parameter is directly given to an array , Therefore, each node can be regarded as a linked list , Then merge every two nodes , Form a new linked list , Merge with the next node , You can achieve the whole K A combination of linked lists . But this method will time out , unable ac. Therefore, the array is divided into two from the middle , Until it is divided into individual nodes . Finally, connect the individual nodes , Get the merged K A linked list .
Code implementation
/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */
class Solution {
public:
ListNode *mergeKLists(vector<ListNode *> &lists) {
return divMerge(lists, 0, lists.size() - 1);
}
// Split the array in half into elements , Re merger
ListNode* divMerge(vector<ListNode*> &lists, int left, int right)
{
if (left > right) return NULL;
if (left == right) return lists[left];
int mid = left + (right - left) / 2;
return mergeTwoList(divMerge(lists, left, mid), divMerge(lists, mid + 1, right));
}
// Merge two linked lists
ListNode* mergeTwoList(ListNode* p1, ListNode* p2)
{
ListNode* ret = new ListNode(0);
ListNode* cur = ret;
while (p1 && p2)
{
if (p1->val < p2->val)
{
cur->next = p1;
p1 = p1->next;
}
else
{
cur->next = p2;
p2 = p2->next;
}
cur = cur->next;
}
if (p1)
{
cur->next = p1;
}
if (p2)
{
cur->next = p2;
}
return ret->next;
}
};
边栏推荐
- [overview of AUTOSAR three RTE]
- 1038 Recover the Smallest Number
- Array and collection performance comparison
- 深度剖析数据在内存中的存储
- Thank you for being together for these extraordinary two years!
- Tensorflow 2. Chapter 15 of X (keras) source code explanation: migration learning and fine tuning
- What is needed to develop a domestic arm intelligent edge computing gateway
- Test shift right: Elk practice of online quality monitoring
- 拥抱平台化交付的安全理念
- leetcode-2280:表示一个折线图的最少线段数
猜你喜欢

Initial order of pointer (basic)

Web2.0 giants have deployed VC, and tiger Dao VC may become a shortcut to Web3

What is needed to develop a domestic arm intelligent edge computing gateway

1.12 - Instructions

安全运营四要素之资产、脆弱性、威胁和事件
![[AUTOSAR + IO Architecture]](/img/cf/9ea42b50bed298c0546764b63bd957.png)
[AUTOSAR + IO Architecture]

数学建模之线性规划(含MATLAB代码)

Unity learns from spaceshooter to record the difference between fixedupdate and update in unity for the second time

Thank you for being together for these extraordinary two years!

tail -f 、tail -F、tailf的区别
随机推荐
【AutoSAR 一 概述】
链表内指定区间反转
全志A40i/T3如何通过SPI转CAN
matlab查找某一行或者某一列在矩阵中的位置
Leetcode-849: maximum distance to the nearest person
[AUTOSAR VI description document]
Advanced pointer (I)
Lu Zhe, chief scientist of Shiping information: building data and personnel centered security capabilities
图解网络:什么是虚拟路由器冗余协议 VRRP?
RK3568开发板评测篇(二):开发环境搭建
465. 最优账单平衡 DFS 回溯
【小程序项目开发-- 京东商城】uni-app之自定义搜索组件(中)-- 搜索建议
Teach you JDBC hand in hand -- structure separation
Explain the basic concepts and five attributes of RDD in detail
Leetcode-871: minimum refueling times
In the first half of 2022, there are 10 worth seeing, and each sentence can bring you strength!
[introduction to AUTOSAR seven tool chain]
465. DFS backtracking of optimal bill balance
Win10 多种方式解决无法安装.Net3.5的问题
[daily training] 871 Minimum refueling times