当前位置:网站首页>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;
}
};
边栏推荐
- Test shift right: Elk practice of online quality monitoring
- 链表内指定区间反转
- RK3568开发板评测篇(二):开发环境搭建
- excel IF公式判断两列是否相同
- leetcode-2280:表示一个折线图的最少线段数
- 1038 Recover the Smallest Number
- [daily training] 871 Minimum refueling times
- Infrared thermography temperature detection system based on arm rk3568
- leetcode-849:到最近的人的最大距离
- Illustrated network: what is virtual router redundancy protocol VRRP?
猜你喜欢
![[shutter] image component (cached_network_image network image caching plug-in)](/img/cc/967ff62c7f82e1c6613b3d0f26bb3e.gif)
[shutter] image component (cached_network_image network image caching plug-in)

File operation io-part2

全志A40i/T3如何通过SPI转CAN

Linear programming of mathematical modeling (including Matlab code)

Lu Zhe, chief scientist of Shiping information: building data and personnel centered security capabilities

FPGA - 7 Series FPGA internal structure clocking -04- multi area clock

Web2.0的巨头纷纷布局VC,Tiger DAO VC或成抵达Web3捷径
![[AUTOSAR nine c/s principle Architecture]](/img/59/ce32c0ff58ef5d8385fe950136175b.png)
[AUTOSAR nine c/s principle Architecture]
![[C language] branch and loop statements (Part 1)](/img/47/6efcc59bd26e26f66c698635c26c8b.png)
[C language] branch and loop statements (Part 1)

Vulkan performance and refinement
随机推荐
Usage of using clause in kingbases alter table
【AutoSAR 三 RTE概述】
2022中国3D视觉企业(引导定位、分拣场景)厂商名单
[overview of AUTOSAR three RTE]
Understanding and distinguishing of some noun concepts in adjustment / filtering
链表中的节点每k个一组翻转
[C language] branch and loop statements (Part 1)
2022 list of manufacturers of Chinese 3D vision enterprises (guided positioning and sorting scenes)
[introduction to AUTOSAR seven tool chain]
[AUTOSAR five methodology]
安全运营四要素之资产、脆弱性、威胁和事件
Vulkan is not a "panacea"“
In the first half of 2022, there are 10 worth seeing, and each sentence can bring you strength!
【AutoSAR 八 OS】
深度剖析数据在内存中的存储
RISA rz/g2l processor introduction | frame diagram | power consumption | schematic diagram and hardware design guide
[AUTOSAR I overview]
Leetcode-934: the shortest Bridge
Lu Zhe, chief scientist of Shiping information: building data and personnel centered security capabilities
Vulkan-性能及精细化