当前位置:网站首页>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;
}
};
边栏推荐
- Annotation
- 面试害怕被问MySQL相关问题 ?这份三万字精华总结 + 面试100 问,吊打面试官完全够了
- 源码阅读 | OpenMesh读取文本格式stl的过程
- STP spanning tree protocol Foundation
- Wechat side: what is consistent hash? In what scenario? What problems have been solved?
- Programmers become gods by digging holes in one year, carrying flags in five years and becoming gods in ten years
- AttacKG: Constructing Technique Knowledge Graph from Cyber Threat Intelligence Reports代码复现
- Can AI chat robots replace manual customer service?
- In the multi network card environment, the service IP registered by Nacos is incorrect, resulting in inaccessible services
- C language operators and expressions
猜你喜欢

High level application of SQL statements in MySQL database (II)

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

JMM 最最最核心的概念:Happens-before 原则

Redis-跳表

Chapter 10 project communication management

NIO、BIO、AIO

ThreadLocal local thread

Huada 04A operating mode / low power consumption mode

2022-06-10 工作记录--JS-获取到某一日期N天后的日期

win10或win11打印机无法打印
随机推荐
CDN principle
Yyds dry goods inventory junit5 learning II: assumptions class
Leetcode: calculate the number of elements less than the current element on the right (sortedlist+bisect\u left)
Fanuc robot_ Introduction to Karel programming (1)
NIO、BIO、AIO
A girl has been making hardware for ten years. 。。
Heavyweight! Fada is listed as a "specialized and new" enterprise
Genesis public chain and a group of encryption investors in the United States gathered in consensus 2022
Kubevela v1.2 release: the graphical operation console velaux you want is finally here
Chapter 10 project communication management
故障安全移动面板KTP900F Mobile下载程序提示无法下载,目标设备正在运行或未处于传输模式的解决办法
Principles of Ethernet port mirroring, link aggregation and VLAN Technology
2022-06-16 work record --js- judge the number of digits in string type digits + judge the number of digits in numeric type digits + limit the text length (display n words at most, exceeding...)
Use of selector for NiO multiplexing
ThreadLocal内存泄漏问题
Principle of IP routing
Certificate photo processing
How to automatically remove all . orig files in Mercurial working tree?
win10或win11打印机无法打印
Technology Review: what is the evolution route of container technology? What imagination space is there in the future?