当前位置:网站首页>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;
}
};
边栏推荐
- Rust string slicing, structs, and enumeration classes
- How to find out the currently running version of Solr- How do I find out version of currently running Solr?
- (C language) data storage
- 【AutoSAR 六 描述文件】
- lex && yacc && bison && flex 配置的问题
- FPGA - 7系列 FPGA内部结构之Clocking -04- 多区域时钟
- 1038 Recover the Smallest Number
- 【AutoSAR 十一 通信相关机制】
- 【AutoSAR 一 概述】
- Meaning of Tencent cloud free SSL certificate extension file
猜你喜欢

2022 list of manufacturers of Chinese 3D vision enterprises (guided positioning and sorting scenes)
[AUTOSAR five methodology]

Understanding and distinguishing of some noun concepts in adjustment / filtering
![[case sharing] let the development of education in the new era advance with](/img/11/af88d16dc66f00840cbfc5ba5d68bd.jpg)
[case sharing] let the development of education in the new era advance with "number"
![[AUTOSAR twelve mode management]](/img/42/292e3da3f5d488a1e8c10ea9bbfbab.png)
[AUTOSAR twelve mode management]

2022中国3D视觉企业(引导定位、分拣场景)厂商名单

File operation io-part2

Win10 can't be installed in many ways Problems with NET3.5

【小程序项目开发-- 京东商城】uni-app之自定义搜索组件(中)-- 搜索建议

图解网络:什么是虚拟路由器冗余协议 VRRP?
随机推荐
leetcode-2280:表示一个折线图的最少线段数
Array and collection performance comparison
matlab查找某一行或者某一列在矩阵中的位置
全志A40i/T3如何通过SPI转CAN
[AUTOSAR nine c/s principle Architecture]
What is needed to develop a domestic arm intelligent edge computing gateway
1.12 - Instructions
leetcode-2115:从给定原材料中找到所有可以做出的菜
About qbytearray storage hexadecimal and hexadecimal conversion
【案例分享】让新时代教育发展与“数”俱进
Rust ownership (very important)
Lu Zhe, chief scientist of Shiping information: building data and personnel centered security capabilities
Vulkan并非“灵药“
Initial order of pointer (basic)
Leetcode-2115: find all the dishes that can be made from the given raw materials
Key detection and sinusoidal signal output developed by Arduino
Win10 can't be installed in many ways Problems with NET3.5
Foundations of data science is free to download
MySQL multi table joint deletion
[overview of AUTOSAR four BSW]