当前位置:网站首页>合并K个已排序的链表
合并K个已排序的链表
2022-07-03 00:29:00 【Schuyler Hu】
问题

思路
由于代码形参是直接给了一个数组,因此可以把每个节点都当成一个链表,然后通过每两个节点合并,构成新的链表,再与下一个节点合并,就可以实现整体K个链表的合并。但是这种方法会超时,无法ac。因此采用从中间将数组一分为二,直到分成一个个单独节点。最后将单独节点连接起来,得到合并后的K个链表。
代码实现
/** * 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);
}
// 将数组对半拆成一个个元素,再合并
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));
}
// 合并两个链表
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;
}
};
边栏推荐
- Solve the cache problem of reactnative using WebView
- cordova-plugin-device获取设备信息插件导致华为审核不通过
- 【AutoSAR 八 OS】
- Shell implements basic file operations (SED edit, awk match)
- Attributeerror: 'tuple' object has no attribute 'layer' problem solving
- 【AutoSAR 十 IO架构】
- Cordova plugin device obtains the device information plug-in, which causes Huawei to fail the audit
- 递归处理组织的几种情况
- Illustrated network: what is virtual router redundancy protocol VRRP?
- Shell implements basic file operations (cutting, sorting, and de duplication)
猜你喜欢
![[AUTOSAR eight OS]](/img/ac/fbc84c077ff9c94c840e1871171d19.png)
[AUTOSAR eight OS]

【AutoSAR 十二 模式管理】

2022.2.14 resumption
![[AUTOSAR 11 communication related mechanism]](/img/bf/834b0fad3a3e5bd9c1be04ba150f98.png)
[AUTOSAR 11 communication related mechanism]

Hdu3507 (slope DP entry)

【AutoSAR 七 工具链简介】

Linux Software: how to install redis service

How to convert Quanzhi a40i/t3 to can through SPI
![[flutter] icons component (load the built-in icon of flutter | display the material design icon completely)](/img/f5/3ec22f1480227f33a1c8ac457155ed.jpg)
[flutter] icons component (load the built-in icon of flutter | display the material design icon completely)

【AutoSAR 一 概述】
随机推荐
leetcode-934:最短的桥
Advanced pointer (I)
Vulkan performance and refinement
【C语言】分支和循环语句(上)
Vulkan is not a "panacea"“
Vulkan practice first bullet
File operation io-part2
[overview of AUTOSAR three RTE]
[shutter] image component (the placeholder | transparent_image transparent image plug-in is loaded into the memory)
Vulkan并非“灵药“
Illustrated network: what is virtual router redundancy protocol VRRP?
leetcode-871:最低加油次数
[applet project development -- JD mall] user defined search component of uni app (middle) -- search suggestions
matlab将数字矩阵保存为地理空间数据出错,显示下标索引必须为正整数类型或逻辑类型,解决
Leetcode-849: maximum distance to the nearest person
世平信息首席科学家吕喆:构建以数据和人员为中心的安全能力
详解RDD基本概念、RDD五大属性
lex && yacc && bison && flex 配置的問題
Leetcode-934: the shortest Bridge
1.12 - Instructions