当前位置:网站首页>Linked list - the first common node of two linked lists
Linked list - the first common node of two linked lists
2022-07-26 18:25:00 【Morita Rinko】
subject :
Enter two acyclic one-way linked lists , Find their first common node , If there is no public node, null is returned .( Note that because the incoming data is a linked list , So the error test data prompt is displayed in other ways , Make sure the incoming data is correct )
for example , Input {1,2,3},{4,5},{6,7} when , The structure of two acyclic one-way linked lists is shown in the figure below :
You can see that the node value of their first common node is 6, Therefore, the returned node value is 6 The node of .
Input description :
Input is divided into yes 3 paragraph , The first paragraph is the non-public part of the first linked list , The second paragraph is the non-public part of the second linked list , The third paragraph is the common part of the first linked list and the second linked list . The backstage will 3 Two parameters are assembled into two linked lists , And the corresponding head nodes of the two linked lists are passed into the function FindFirstCommonNode Inside , The only input the user gets is pHead1 and pHead2.
Return value description :
Return the incoming pH*=ead1 and pHead2 First common node of , The linked list with this node as the head node will be printed in the background .
Their thinking :
Because we are looking for the first common node of the two linked lists , So our first thought may be to traverse the linked list , Find the first public node , But this method has two cycles , The time complexity will be high , We hope to find a method of linear time complexity to solve the problem .
If you want to solve the problem with linear time complexity , There is an ideal state : The length of the two linked lists is the same .
In this case , We can separately from the linked list a And the list b The head node of starts traversing , Until the first common node appears , Return the node , Otherwise, there is no public node , At this time, the pointer just goes to the end of the linked list , Pointer to nullptr, return nullptr.
however , The length of the input list of topics is not the same , So we try to construct them into an ideal state , Suppose the linked list a The length is m, Linked list b The length is n,m!=n, however m+n=n+m, So we can link the linked lists with each other .
Whether the link is in front or behind , Because the length is the same , From the end of the linked list , Will be the end of two original lists ,
- If there are public nodes , Then from the tail, it corresponds one by one , We just need to traverse from front to back , Just find the first public node .
- If there is no public node , Then there is no one-to-one corresponding node from the tail , Traverse from front to back until the end , return nullptr that will do .
Solution code :
/*
struct ListNode {
int val;
struct ListNode *next;
ListNode(int x) :
val(x), next(NULL) {
}
};*/
class Solution {
public:
ListNode* FindFirstCommonNode( ListNode* pHead1, ListNode* pHead2) {
ListNode* p1=pHead1,*p2=pHead2;
// Until we finally find the common node or reach the tail of the synthetic linked list
while(p1!=p2){
p1=p1?p1->next:pHead2;// If p1 Traverse to the tail , From the linked list 2 Start and then traverse
p2=p2?p2->next:pHead1;// If p2 Traverse to the tail , From the linked list 1 Start and then traverse
}
return p1;
}
};
边栏推荐
猜你喜欢

Kindergarten system based on SSM

IrrKlang音频库的下载和配置

How about the employment prospects of Russian translation? How to do a good job of Russian translation

俄语翻译的就业前景怎样 如何做好俄语翻译工作

If the recommendation effect is not satisfactory, it's better to try to learn the propeller chart

【数字IC】深入浅出理解AXI-Lite协议

分布式链路追踪Jaeger在Golang中的使用

剑指offer 连续子数组的最大和(二)

Leetcode 50 day question brushing plan (day 5 - longest palindrome substring 10.50-13:00)

Sign up now | cloud native technology exchange meetup Guangzhou station has been opened, and I will meet you on August 6!
随机推荐
If the recommendation effect is not satisfactory, it's better to try to learn the propeller chart
ssm练习第三天_分页助手_安全框架
Redis主从复制,读写分离,哨兵模式
跟我学 UML 系统建模
Nailing third-party service provider application ISV application development and listing tutorial
Relative path and absolute path
Maximum sum of continuous subarray of sword finger offer (2)
钉钉第三方服务商应用ISV应用开发及上架教程
隐私计算基础组件系列-混淆电路
ALV屏幕输入选项学习
J9数字论:如何避免踩雷多头陷阱?
效率提升98%!高海拔光伏电站运维巡检背后的AI利器
数据仓库:详解维度建模之事实表
Leetcode 50 day question brushing plan (day 2 - the longest substring without repeated characters 10.00-12.00)
面试OPPO,16道题甩过来,我人傻了
网易游戏研发工程师实习生(客户端方向)一面
OpenWrt之feeds.conf.default详解
【数字IC】深入浅出理解AXI-Lite协议
quartz触发器规则
Leetcode 0137. number II that appears only once