当前位置:网站首页>Merge k sorted linked lists
Merge k sorted linked lists
2022-08-10 22:31:00 【dry rice white】
ojbk...At first I thought there was no memory requirement for this question,So use a very simple method to merge.Create a third linked list,As a result, some cases failed.
这个代码:
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* mergetK(ListNode *A,ListNode *B)
{
ListNode *C = nullptr;
ListNode *ans = nullptr;
while(A != nullptr && B != nullptr)
{
ListNode *s = new ListNode(0);
if(A->val > B->val)
{
s->val = B->val;
s->next = nullptr;
B = B->next;
}
else
{
s->val = A->val;
s->next = nullptr;
A = A->next;
}
if(C == nullptr)
{
C = s;
ans = C;
}
else
{
C->next = s;
C = C->next;
}
}
while(A != nullptr)
{
ListNode *s = new ListNode(0);
s->val = A->val;
s->next = nullptr;
if(C == nullptr)
{
C = s;
ans = C;
}
else
{
C->next = s;
C = C->next;
}
A = A->next;
}
while(B != nullptr)
{
ListNode *s = new ListNode(0);
s->val = B->val;
s->next = nullptr;
if(C == nullptr)
{
C = s;
ans = C;
}
else
{
C->next = s;
C = C->next;
}
B = B->next;
}
return ans;
}
ListNode *mergeKLists(vector<ListNode *> &lists) {
if(lists.empty())
{
return nullptr;
}
ListNode *ans;
ans = lists[0];
for(int i=1;i<lists.size();i++)
{
ans = mergetK(ans,lists[i]);
}
return ans;
}
};
好吧,It appears that new nodes cannot be created,Only auxiliary pointers can be used.....(This problem is to merge two sorted linked lists,水题)
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* mergetK(ListNode *A,ListNode *B)
{
ListNode *pA = A;
ListNode *pB = B;
ListNode *ans = nullptr;
ListNode *head = nullptr;
while(A != nullptr && B != nullptr)
{
if(A->val <= B->val)
{
if(ans == nullptr)
{
ans = A;
head = A;
}
else
{
head->next = A;
head = head->next;
}
//pA = A;
A = A->next;
}
else
{
if(ans == nullptr)
{
ans = B;
head = B;
}
else
{
head->next = B;
head = head->next;
}
//pB = B;
B = B->next;
}
}
if(A != nullptr)
{
head->next = A;
}
if(B != nullptr)
{
head->next = B;
}
return ans;
}
ListNode *mergeKLists(vector<ListNode *> &lists) {
if(lists.empty())
{
return nullptr;
}
ListNode *ans;
ans = lists[0];
for(int i=1;i<lists.size();i++)
{
if(lists[i] == nullptr)
{
continue;
}
ans = mergetK(ans,lists[i]);
}
return ans;
}
};
我去,超时.....
It seems that this question is not as simple as I thought....
Analyze the time complexity of the code,,,,
遍历 litsts O(n)
内部:It's like traversing it once 链表 O(n+m)
这么算:O(n^2)
It depends on the time complexity O(nlogk)
kis the number of linked lists:说明只能从 遍历 litsts开了....
根据经验,The time complexity of merging two sorted linked lists is optimal O(n+m) 再一次说明 只能从 遍历 litsts优化了
等等,logK对数时间,The first reaction should be two points.我去,说不定可以解决
二分的前提:保证有序,nice,The conditions of the question are just in order(Because array subscripts are incremented)
优化后:
我操,终于过了.....感动,落泪.An hour woohoo....
两个链表的合并:(The previous code is a bit smallbug,There is one situation that requires special handling)
When a linked list is empty at the end:(Merging should not be required at this point)
[{-9},{},{-10,-8,-2,-1,2},{-10,-9,-7,-6,-5,-2,-2,1,4},{-7,-7,-1,0,4},{},{-4,0},{-9,-6,-5,-5,-2,2,3,3}]
更改:Make a special judgment,The merging of two ordered linked lists will not be mentioned,As long as you are serious, you can write it.
Merge two ordered linked list codes:
struct ListNode
{
int val;
ListNode *next;
ListNode(int x) : val(x), next(NULL) {}
};
ListNode* merge(ListNode *A,ListNode *B)
{
//特判 (A=nullptr && B=nullptr) 或者
// (A=nullptr && B!=nullptr) 或者 (A!=nullptr && B=nullptr)
if(A == nullptr || B == nullptr)
{
return A==nullptr ? B:A;
}
ListNode *head = nullptr;
ListNode *ans = nullptr;
while(A != nullptr && B != nullptr)
{
if(A->val <= B->val)
{
if(ans == nullptr)
{
ans = A;
head = A;
}
else
{
head->next = A;
head = head->next;
}
A = A->next;
}
else
{
if(ans == nullptr)
{
ans = B;
head = B;
}
else
{
head->next = B;
head = head->next;
}
B = B->next;
}
}
if(A != nullptr)
{
head->next = A;
}
if(B != nullptr)
{
head->next = B;
}
retrun ans;
}
Dichotomous ideas and codes:
There are two methods for bisection:递归(推荐使用) 非递归
Usually we just use recursion,好分析,好写代码
Let's look at an example to analyze:
Divide in half first: A和BThe process must be the same(Or divide a whole into two subproblems that are the same as the whole)
假设将A和Binto the smallest sub-problem,What should we do now????
Of course, the linked list is merged....
OK, let's merge,又看,A和BIt has not been divided to the minimum.(Recursion is a top-down process)
Why am I here to sayA和BConsider it the smallest sub-problem??This is the author's special understanding of recursion,If the solution space is drawn,Recursion does not require backtracking,Does it need to be merged when we go back to this level A和B.
ok....For ease of understanding, we might as well treat each state as a minimal problem,What is the solution to the minimum problem,We'll just do what we do,And the number is divided into the same problem as the sub-problem,It's just a scale traversal.
我们继续划分
Let's write code(二分代码)
//The return value looks at the picture I drew above
ListNode* binary_lists(vector<ListNode *> &lists,int l,int r)
{
//先写边界条件(出口)
if(l == r)
{
return lists[l];
}
if(l > r)
{
return nullptr;
}
int mid = (l+r)/2;
//合并 A和B Because backtracking to this point requires mergingA和B
return merge(binary_lists(lists,l,mid),binary_lists(lists,mid+1,r));
}
okk...
Let's put all the code together
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode *ans1;
ListNode* merget(ListNode *A,ListNode *B)
{
if(A == nullptr || B == nullptr)
{
return A == nullptr ? B:A;
}
ListNode *pA = A;
ListNode *pB = B;
ListNode *ans = nullptr;
ListNode *head = nullptr;
while(A != nullptr && B != nullptr)
{
if(A->val <= B->val)
{
if(ans == nullptr)
{
ans = A;
head = A;
}
else
{
head->next = A;
head = head->next;
}
//pA = A;
A = A->next;
}
else
{
if(ans == nullptr)
{
ans = B;
head = B;
}
else
{
head->next = B;
head = head->next;
}
//pB = B;
B = B->next;
}
}
if(A != nullptr)
{
head->next = A;
}
if(B != nullptr)
{
head->next = B;
}
return ans;
}
ListNode* merList(vector<ListNode *> &lists,int l,int r)
{
if(l == r) //只有一条
{
return lists[l];
}
if( l > r)
{
return nullptr;
}
int mid = (l + r)/2;
return merget(merList(lists,l,mid),merList(lists,mid+1,r));
}
ListNode *mergeKLists(vector<ListNode *> &lists) {
if(lists.empty())
{
return nullptr;
}
// ans = lists[0];
// for(int i=1;i<lists.size();i++)
// {
// if(lists[i] == nullptr)
// {
// continue;
// }
// ans = mergetK(ans,lists[i]);
// }
return merList(lists,0,lists.size()-1);
}
};
小结:创作不易,Click to support it
如果有喜欢C++The students can view the author's other columns,全套C++都在更新(从C++基础到Linux系 system programming、网络编程 Data programming, etc... And there is a completeC++项目)
边栏推荐
- LeetCode Daily Question (1573. Number of Ways to Split a String)
- Exploration and practice of the "zero trust" protection and data security governance system of the ransomware virus of Meichuang Technology
- camera preview process --- from HAL to OEM
- ASCII、Unicode和UTF-8
- 3598. 二叉树遍历(华中科技大学考研机试题)
- 黑猫带你学Makefile第13篇:Makefile编译问题合集
- Live Classroom System 08 Supplement - Tencent Cloud Object Storage and Course Classification Management
- Black cats take you learn Makefile article 13: a Makefile collection compile problem
- 【开源教程5】疯壳·开源编队无人机-飞控固件烧写
- 高通平台开发系列讲解(应用篇)QCMAP应用框架介绍
猜你喜欢
3598. 二叉树遍历(华中科技大学考研机试题)
What is Jmeter? What are the principle steps used by Jmeter?
【Maui正式版】创建可跨平台的Maui程序,以及有关依赖注入、MVVM双向绑定的实现和演示
HighTec shortcut keys (Keys) setting location
Thread State 详解
C#【必备技能篇】Hex文件转bin文件的代码实现
深度学习之 12 循环神经网络RNN2
geemap的详细安装步骤及环境配置
【PCBA scheme design】Bluetooth skipping scheme
威纶通触摸屏如何在报警的同时,显示出异常数据的当前值?
随机推荐
威纶通触摸屏如何在报警的同时,显示出异常数据的当前值?
如何成为一名正义黑客?你应该学习什么?
C#【必备技能篇】Hex文件转bin文件的代码实现
APP UI自动化测试常见面试题,或许有用呢~
STL-stack
[SQL brush questions] Day3----Special exercises for common functions that SQL must know
c语言之 练习题1 大贤者福尔:魔法数,神奇的等式
FPGA - Memory Resources of 7 Series FPGA Internal Structure -03- Built-in Error Correction Function
解码2022中国网安强星丨正向建、反向查,华为构建数字化时代的网络安全防线
shell脚本
美味石井饭菜
新一代网络安全防护体系的五个关键特征
链表相加(二)
翻译科技论文,俄译中怎样效果好
SDP
About DataFrame: Processing Time
Application of Spatial 3D Model Reconstruction Based on Pix4Dmapper - Spatial Analysis and Site Selection
What are the concepts, purposes, processes, and testing methods of interface testing?
电力系统潮流计算(牛顿-拉夫逊法、高斯-赛德尔法、快速解耦法)(Matlab代码实现)
The perfect alternative to domestic Gravatar avatars Cravatar