当前位置:网站首页>[sword finger offer] 52 The first common node of two linked lists
[sword finger offer] 52 The first common node of two linked lists
2022-06-30 17:40:00 【LuZhouShiLi】
The finger of the sword Offer 52. The first common node of two linked lists
subject
Enter two linked lists , Find their first common node .
Ideas
Using two Pointers Node1,node2, Point to two linked lists respectively headA,headB The head node of , Then traverse node by node at the same time , When node1 Reach the linked list headA At the end of , Relocate to the linked list headB The head node of , When node2 Reach the linked list headB At the end of , Relocate to the linked list headA The head node of .
The lengths of the two linked lists are L1 + C, L2 + C, C Is the length of the common part , So the first pointer goes L1 + C After step , Go back to the starting point of the second linked list L2 Step , The second man left L1 Step , When the second person left l2+C After step , Go back to the first person L1 Step , When two people take the same number of steps L1 + L2 + C after , Just met .
Code
/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */
class Solution {
public:
ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {
ListNode *node1 = headA;
ListNode *node2 = headB;
while(node1 != node2)
{
node1 = node1 != NULL ? node1->next : headB;
node2 = node2 != NULL ? node2->next: headA;
}
return node1;
}
};
边栏推荐
- Network: principle and practice of server network card group technology
- unity粒子_异常显示处理
- [200 opencv routines] 215 Drawing approximate ellipse based on polyline
- Radio and television 5g officially set sail, attracting attention on how to apply the golden band
- Map collection
- MySQL advanced notes
- 5g has been in business for three years. Where will innovation go in the future?
- EMQ helps Qingdao Yanbo build a smart water platform
- Ten thousand volumes - list sorting [01]
- Exercise book of introduction to database system
猜你喜欢

Interview shock 60: what will cause MySQL index invalidation?

Cesium-1.72 learning (eagle eye map of the earth)

Key to understanding the trend of spot Silver

3D图表有效提升数据大屏档次

期未课程设计:基于SSM的产品销售管理系统

Compile and generate busybox file system
![[zero basic IOT pwn] environment construction](/img/3b/a0689a1570fcc40bb9a5a4e9cdc63c.png)
[zero basic IOT pwn] environment construction

Parker Parker sensor p8s-grflx
![[零基础学IoT Pwn] 环境搭建](/img/3b/a0689a1570fcc40bb9a5a4e9cdc63c.png)
[零基础学IoT Pwn] 环境搭建

Spin lock exploration
随机推荐
canvas云朵形状动画
How can you choose to work in the county after graduation?
Property or method “approval1“ is not defined on the instance but referenced during render
[零基础学IoT Pwn] 环境搭建
leetcode:1042. 不邻接植花【随机填入符合要求的 + 后面不会形成矛盾 + set.pop】
Shutter music recording playing audioplayers
零基础也能做Apple大片!这款免费工具帮你渲染、做特效、丝滑展示
[proteus simulation] Arduino uno uses 74ls148 to extend interrupt
Unity particle_ Exception display processing
开发那些事儿:如何在视频中添加文字水印?
Radio and television 5g officially set sail, attracting attention on how to apply the golden band
Cesium-1.72 learning (eagle eye map of the earth)
理解现货白银走势的关键
[C language] detailed explanation of threads - multi threads for collaborative operation
Hyper-V: enable SR-IOV in virtual network
Exch:完整性检查 Database Integrity Checking
parker比例溢流阀RS10R35S4SN1JW
5G业务正式商用,属于广电的机会在哪?
3D图表有效提升数据大屏档次
Horizontal visual error effect JS special effect code