当前位置:网站首页>合并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;
}
};
边栏推荐
- University of Oslo: Li Meng | deep reinforcement learning based on swing transformer
- matlab将数字矩阵保存为地理空间数据出错,显示下标索引必须为正整数类型或逻辑类型,解决
- kubernetes资源对象介绍及常用命令(五)-(NFS&PV&PVC)
- [AUTOSAR five methodology]
- Linux Software: how to install redis service
- Leetcode-224: basic calculator
- [shutter] image component (load network pictures | load static pictures | load local pictures | path | provider plug-in)
- 详解RDD基本概念、RDD五大属性
- [flutter] icons component (load the built-in icon of flutter | display the material design icon completely)
- [AUTOSAR eight OS]
猜你喜欢
![[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)

University of Oslo: Li Meng | deep reinforcement learning based on swing transformer

2022上半年值得被看见的10条文案,每一句都能带给你力量!

Basic use of sringcloud & use of component Nacos

【AutoSAR 七 工具链简介】

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

Win10 can't be installed in many ways Problems with NET3.5
![[AUTOSAR II appl overview]](/img/da/76ccc05e2199705b20d8304bfb86b2.png)
[AUTOSAR II appl overview]

Leetcode-2280: represents the minimum number of line segments of a line graph

leetcode-849:到最近的人的最大距离
随机推荐
An excellent orm in dotnet circle -- FreeSQL
Shell implements basic file operations (SED edit, awk match)
[AUTOSAR XIII NVM]
[AUTOSAR eight OS]
详解RDD基本概念、RDD五大属性
2022.2.14 resumption
RISA rz/g2l processor introduction | frame diagram | power consumption | schematic diagram and hardware design guide
leetcode-2280:表示一个折线图的最少线段数
[AUTOSAR 11 communication related mechanism]
Meaning of Tencent cloud free SSL certificate extension file
Unity learns from spaceshooter to record the difference between fixedupdate and update in unity for the second time
lex && yacc && bison && flex 配置的問題
Overlay of shutter (Pop-Up)
Web2.0的巨头纷纷布局VC,Tiger DAO VC或成抵达Web3捷径
465. 最优账单平衡 DFS 回溯
Extension of flutter
Arduino开发之按键检测与正弦信号输出
[AUTOSAR five methodology]
Basic use of sringcloud & use of component Nacos
lex && yacc && bison && flex 配置的问题