当前位置:网站首页>[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;
}
};
边栏推荐
- 网络:服务器网卡组技术原理与实践
- Ningx 1.20.2
- [C language] explain threads - thread separation function pthread_ detach
- 3D chart effectively improves the level of large data screen
- unity粒子_异常显示处理
- 5g has been in business for three years. Where will innovation go in the future?
- TFTP download kernel, NFS mount file system
- Booking UI effect implemented by svg
- 理解现货白银走势的关键
- Cesium-1.72 learning (eagle eye map of the earth)
猜你喜欢

商鼎雲新版來襲 | 收藏夾功能已上線,滿足個人使用需求

Tubes响应性数据系统的设计与原理
![[200 opencv routines] 215 Drawing approximate ellipse based on polyline](/img/43/fd4245586071020e5aadb8857316c5.png)
[200 opencv routines] 215 Drawing approximate ellipse based on polyline

【C语言】详解线程 — 线程分离函数 pthread_detach

24: Chapter 3: develop pass service: 7: user defined exceptions (to represent errors in the program); Create graceexceptionhandler to handle exceptions globally and uniformly (build JSON data of corre

编写C语言的最简单小程序Hello world

Canvas cloud shape animation

Cesium-1.72 learning (earth rotation)

每日刷题记录 (九)

Ningx 1.20.2
随机推荐
leetcode:1042. 不邻接植花【随机填入符合要求的 + 后面不会形成矛盾 + set.pop】
JS from prototype chain to inheritance
Shutter music recording playing audioplayers
网络:服务器网卡组技术原理与实践
MySQL advanced notes
送受伤婴儿紧急就医,滴滴司机连闯五个红灯
Ningx 1.20.2
6 張圖帶你搞懂 TCP 為什麼是三次握手?
Ten thousand volumes - list sorting [01]
5G业务正式商用,属于广电的机会在哪?
Exch: database integrity checking
Exch:完整性检查 Database Integrity Checking
Map集合
AcWing 第 57 场周赛
Canvas mouse control gravity JS effect
登录框Tricks
Property or method “approval1“ is not defined on the instance but referenced during render
[untitled] write a student achievement and information management system in C language to realize the operation interface, clear screen display of current operation functions, reading and inputting st
【C语言】详解线程 — 开启两个线程
China Infrastructure Development Association: electronic contract is recommended