当前位置:网站首页>合并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;
}
};
边栏推荐
- [AUTOSAR XIII NVM]
- Shell implements basic file operations (cutting, sorting, and de duplication)
- Leetcode-871: minimum refueling times
- KingbaseES ALTER TABLE 中 USING 子句的用法
- 1.12 - Instructions
- Tensorflow 2.x(keras)源码详解之第十五章:迁移学习与微调
- 文件操作IO-Part2
- leetcode-849:到最近的人的最大距离
- [AUTOSAR VI description document]
- 【JetCache】JetCache的配置说明和注解属性说明
猜你喜欢

瑞萨电子RZ/G2L开发板上手评测

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

Hdu3507 (slope DP entry)

【AutoSAR 十三 NVM】

Basic use of sringcloud & use of component Nacos

Is there a free text to speech tool to help recommend?

Rk3568 development board evaluation (II): development environment construction

Win10 多种方式解决无法安装.Net3.5的问题

RISA rz/g2l processor introduction | frame diagram | power consumption | schematic diagram and hardware design guide

In the first half of 2022, there are 10 worth seeing, and each sentence can bring you strength!
随机推荐
leetcode-2280:表示一个折线图的最少线段数
cordova-plugin-device获取设备信息插件导致华为审核不通过
解决ReactNative使用webView存在缓存问题
Web2.0的巨头纷纷布局VC,Tiger DAO VC或成抵达Web3捷径
An excellent orm in dotnet circle -- FreeSQL
Vulkan is not a "panacea"“
利亚德:Micro LED 产品消费端首先针对 100 英寸以上电视,现阶段进入更小尺寸还有难度
[overview of AUTOSAR four BSW]
Infrared thermography temperature detection system based on arm rk3568
Lu Zhe, chief scientist of Shiping information: building data and personnel centered security capabilities
[jetcache] jetcache configuration description and annotation attribute description
1038 Recover the Smallest Number
Machine learning: numpy version linear regression predicts Boston house prices
Nc17059 queue Q
Liad: the consumer end of micro LED products is first targeted at TVs above 100 inches. At this stage, it is still difficult to enter a smaller size
深度剖析数据在内存中的存储
leetcode-871:最低加油次数
递归处理组织的几种情况
Problèmes de configuration lex & yacc & Bison & Flex
Lex & yacc & bison & flex configuration problems