当前位置:网站首页>Leetcode algorithm The first common node of two linked lists
Leetcode algorithm The first common node of two linked lists
2022-06-24 22:44:00 【Alex_ 12 hours a day 6 days a week】
Topic link : The finger of the sword Offer 52. The first common node of two linked lists
Ideas
Algorithm : Double pointer + mathematics
data structure : Linked list
Ideas : I remember talking about this problem in the elementary class of Zuoshen algorithm , A little forgotten , The way to do this is to move both hands forward , If pa At the end, move to pb, If pb At the end, move to pa, I'm sure to meet you in the end , But the proof of why we met is a little forgotten . The algorithm I provide here is two pointers plus a little math .First consider the boundary case , If one of the two pointers is null , Then there must be no intersecting nodes , direct
return nullptrJust OK 了 .
Then you need to calculate the length of the two linked lists lenA and lenB And the length difference diff, Then we need to redefine , Give Way A Represents a long linked list ,B Indicates a short linked list .
If two linked lists have intersecting parts , So the long piece must be in the disjoint part , Because after the conversion A A long , So the point to A The pointer of can go first diff Step , Then the two pointers go together , If it's equal , It means that the first intersection node is found , Otherwise, it means that the two linked lists have no intersecting parts .
Code
C++
class Solution {
public:
ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {
if (headA == nullptr || headB == nullptr) {
return nullptr;
}
int lenA = 0, lenB = 0;
ListNode *pA = headA, *pB = headB;
while (pA != nullptr) {
lenA++;
pA = pA->next;
}
while (pB != nullptr) {
lenB++;
pB = pB->next;
}
int diff = abs(lenA - lenB);
// If linked list B Is longer than the linked list A The length of , In exchange for A、B
if (lenB > lenA) {
pA = headA;
headA = headB;
headB = pA;
}
// headA Go ahead diff Step
for (int i = 0; i < diff; i++) {
headA = headA->next;
}
while (headA != nullptr) {
if (headA == headB) {
return headA;
} else {
headA = headA->next;
headB = headB->next;
}
}
return nullptr;
}
};
边栏推荐
- 使用Aggregated APIServer扩展你的kubernetes API
- Envoy obtain the real IP address of the client
- Concurrency of heap memory allocation
- Cross border e-commerce, early entry and early benefit
- Virtual private network foundation
- NIO多路复用之Selector的使用
- Docker installs redis-5.0.12. Detailed steps
- CDN principle
- Technology inventory: past, present and future of Message Oriented Middleware
- Certificate photo processing
猜你喜欢

MySQL + JSON = King fried!!

AQS source code analysis

See how sparksql supports enterprise level data warehouse
CA Zhouji - the first lesson in 2022 rust

2022-06-10 work record --js- obtain the date n days after a certain date

Database transaction Transanction

How to compare two or more distributions: a summary of methods from visualization to statistical testing

Description of software version selection of kt6368a Bluetooth dual-mode transparent chip
Relationnet++: a representation of fusion of multiple detection targets based on transformer | neurips 2020
![[QT] QT event handling](/img/48/14a5491307fee1c99434d6cb308337.jpg)
[QT] QT event handling
随机推荐
Chapter 10 project stakeholder management
Web security XSS foundation 06
Why can some programmers get good offers with average ability?
Chapter 10 project communication management
Extend your kubernetes API with aggregated apiserver
中国SSD行业企业势力全景图
ThreadLocal local thread
证件照处理
结合源码剖析Oauth2分布式认证与授权的实现流程
Nuscenes -- remedies for missing image files or 0-size images encountered during dataset configuration
Power system | IEEE paper submission process
【Mongodb】READ_ME_TO_RECOVER_YOUR_DATA,数据库被恶意删除
Feign project construction
Ideal L9, new trend of intelligent cockpit
Can AI chat robots replace manual customer service?
In the era of full programming, should I give up this road?
【軟件工程】期末重點
故障安全移动面板KTP900F Mobile下载程序提示无法下载,目标设备正在运行或未处于传输模式的解决办法
Industrial development status of virtual human
Programmers become gods by digging holes in one year, carrying flags in five years and becoming gods in ten years