当前位置:网站首页>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;
}
};
边栏推荐
- nuScenes——数据集配置过程中遇到图像文件缺失或大小为0时的补救方法
- CA Zhouji - the first lesson in 2022 rust
- Creating files, recursively creating directories
- The usage difference between isempty and isblank is so different that so many people can't answer it
- Seven principles of software design
- 网上立案流程
- Leetcode: push domino (domino simulation)
- 【软件工程】期末重点
- Use of selector for NiO multiplexing
- Envoy obtain the real IP address of the client
猜你喜欢

电力系统| IEEE论文投稿流程

Docker installs redis-5.0.12. Detailed steps

ThreadLocal内存泄漏问题

Can AI chat robots replace manual customer service?

Future development of education industry of e-commerce Express

Redis hop table

FANUC机器人_KAREL编程入门学习(1)

ThreadLocal memory leak

Creating files, recursively creating directories

结构体的内存对齐
随机推荐
使用Aggregated APIServer扩展你的kubernetes API
【个人实验报告】
Feign project construction
Yyds dry goods inventory junit5 learning II: assumptions class
Why can some programmers get good offers with average ability?
NIO、BIO、AIO
Idea global search replace shortcut key
如何比较两个或多个分布:从可视化到统计检验的方法总结
The difference between get and post
磁盤的結構
Future development of education industry of e-commerce Express
Panorama of enterprise power in China SSD industry
Kubevela v1.2 release: the graphical operation console velaux you want is finally here
Technology Review: what is the evolution route of container technology? What imagination space is there in the future?
ACL (access control list) basic chapter - Super interesting learning network
NiO zero copy
堆內存分配的並發問題
NIO多路复用之Selector的使用
Technology inventory: Technology Evolution and Future Trend Outlook of cloud native Middleware
Process communication mode