当前位置:网站首页>LeetCode Algorithm 剑指 Offer 52. 两个链表的第一个公共节点
LeetCode Algorithm 剑指 Offer 52. 两个链表的第一个公共节点
2022-06-24 19:38:00 【Alex_996】
题目链接:剑指 Offer 52. 两个链表的第一个公共节点
Ideas
算法:双指针+数学
数据结构:链表
思路:这道题我记得在左神算法初级班的时候讲过,有点忘记了,做法是两个指针都往前走,如果pa到头了就挪到pb,如果pb到头了就挪到pa,最后肯定会相遇,但是为什么会相遇的证明有点忘记了。我这里提供的算法是双指针加上一点点数学。首先考虑边界情况,如果两个指针中有一个为空,那么肯定没有相交的节点,直接
return nullptr就OK了。
然后需要计算出两个链表的长度lenA和lenB以及长度差diff,然后需要重新定义一下,让A表示长链表,B表示短链表。
如果两个链表有相交的部分,那么长出来的一块肯定是在不相交的那一部分,因为转换完了之后A比较长,所以指向A的指针可以先走diff步,然后两个指针再一起走,如果相等了,那就说明找到了第一个相交的节点,否则说明两个链表没有相交的部分。
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);
// 如果链表B的长度大于链表A的长度,交换A、B
if (lenB > lenA) {
pA = headA;
headA = headB;
headB = pA;
}
// headA先走diff步
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;
}
};
边栏推荐
- Technology inventory: Technology Evolution and Future Trend Outlook of cloud native Middleware
- Data communication and physical network
- Main steps of system test
- Publicity of the second batch of shortlisted enterprises! Annual Top100 smart network supplier selection
- 详细了解关于sentinel的实际应用
- How to automatically remove all . orig files in Mercurial working tree?
- Seven principles of software design
- MySQL + JSON = King fried!!
- 揭秘B站,程序员穿女装敲代码,效率更高是真的吗?
- Ansible basic configuration
猜你喜欢

Heartless sword Chinese English bilingual poem 003 The sea of books

Servlet details

2022-06-16 工作记录--JS-判断字符串型数字有几位 + 判断数值型数字有几位 + 限制文本长度(最多展示n个字,超出...)
![[personal experiment report]](/img/04/c9e1bee19bff9d55b73c531f7b17f4.png)
[personal experiment report]

YGG 近期游戏合作伙伴一览

Zero code can apply data visualization to enterprise management

NIO 零拷贝

nuScenes——数据集配置过程中遇到图像文件缺失或大小为0时的补救方法

重磅!法大大上榜“专精特新”企业

网上立案流程
随机推荐
Embedded development: tips and tricks -- clean jump from boot loader to application code
envoy获取客户端真实IP
In the era of industrial Internet, there is no Internet in the traditional sense
Redis-跳表
Uncover the secret of station B. is it true that programmers wear women's clothes and knock code more efficiently?
上新了,华为云开天aPaaS
[QT] QT event handling
Data center basic network platform
Why can some programmers get good offers with average ability?
【软件工程】期末重点
Concurrency of heap memory allocation
Extend your kubernetes API with aggregated apiserver
A girl has been making hardware for ten years. 。。
Learn more about the practical application of sentinel
问题求解——嵌套列表
CSRF and SSRF for web attacks
重磅!法大大上榜“专精特新”企业
In the era of full programming, should I give up this road?
Short video mall system, how does scroll view adapt to the remaining height of the page
Cross border e-commerce, early entry and early benefit