forked from youngyangyang04/leetcode-master
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
fea16ee
commit a634dbd
Showing
3 changed files
with
134 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
|
||
<p align="center"> | ||
<a href="https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ"><img src="https://img.shields.io/badge/知识星球-代码随想录-blue" alt=""></a> | ||
<a href="https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw"><img src="https://img.shields.io/badge/刷题-微信群-green" alt=""></a> | ||
<a href="https://img-blog.csdnimg.cn/20201210231711160.png"><img src="https://img.shields.io/badge/公众号-代码随想录-brightgreen" alt=""></a> | ||
<a href="https://space.bilibili.com/525438321"><img src="https://img.shields.io/badge/B站-代码随想录-orange" alt=""></a> | ||
</p> | ||
<p align="center"><strong>欢迎大家参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!</strong></p> | ||
|
||
## 面试题 02.07. 链表相交 | ||
|
||
题目链接:https://leetcode-cn.com/problems/intersection-of-two-linked-lists-lcci/ | ||
|
||
给定两个(单向)链表,判定它们是否相交并返回交点。请注意相交的定义基于节点的引用,而不是基于节点的值。换句话说,如果一个链表的第k个节点与另一个链表的第j个节点是同一节点(引用完全相同),则这两个链表相交。 | ||
|
||
示例 1: | ||
|
||
输入:listA = [4,1,8,4,5], listB = [5,0,1,8,4,5] | ||
|
||
输出:Reference of the node with value = 8 | ||
|
||
输入解释:相交节点的值为 8 (注意,如果两个列表相交则不能为 0)。从各自的表头开始算起,链表 A 为 [4,1,8,4,5],链表 B 为 [5,0,1,8,4,5]。在 A 中,相交节点前有 2 个节点;在 B 中,相交节点前有 3 个节点。 | ||
|
||
|
||
## 思路 | ||
|
||
本来很简洁明了的一道题,让题目描述搞的云里雾里的。 | ||
|
||
简单来说,就是求两个链表交点节点的**指针**。 这里同学们要注意,交点不是数值相等,而是指针相等。 | ||
|
||
为了方便举例,假设节点元素数值相等,则节点指针相等。 | ||
|
||
看如下两个链表,目前curA指向链表A的头结点,curB指向链表B的头结点: | ||
|
||
![面试题02.07.链表相交_1](https://code-thinking.cdn.bcebos.com/pics/%E9%9D%A2%E8%AF%95%E9%A2%9802.07.%E9%93%BE%E8%A1%A8%E7%9B%B8%E4%BA%A4_1.png)v | ||
|
||
我们求出两个链表的长度,并求出两个链表长度的差值,然后让curA移动到,和curB 末尾对齐的位置,如图: | ||
|
||
![面试题02.07.链表相交_2](https://code-thinking.cdn.bcebos.com/pics/%E9%9D%A2%E8%AF%95%E9%A2%9802.07.%E9%93%BE%E8%A1%A8%E7%9B%B8%E4%BA%A4_2.png) | ||
|
||
此时我们就可以比较curA和curB是否相同,如果不相同,同时向后移动curA和curB,如果遇到curA == curB,则找到焦点。 | ||
|
||
否则循环退出返回空指针。 | ||
|
||
C++代码如下: | ||
|
||
```C++ | ||
class Solution { | ||
public: | ||
ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) { | ||
ListNode* curA = headA; | ||
ListNode* curB = headB; | ||
int lenA = 0, lenB = 0; | ||
while (curA != NULL) { // 求链表A的长度 | ||
lenA++; | ||
curA = curA->next; | ||
} | ||
while (curB != NULL) { // 求链表B的长度 | ||
lenB++; | ||
curB = curB->next; | ||
} | ||
curA = headA; | ||
curB = headB; | ||
// 让curA为最长链表的头,lenA为其长度 | ||
if (lenB > lenA) { | ||
swap (lenA, lenB); | ||
swap (curA, curB); | ||
} | ||
// 求长度差 | ||
int gap = lenA - lenB; | ||
// 让curA和curB在同一起点上(末尾位置对齐) | ||
while (gap--) { | ||
curA = curA->next; | ||
} | ||
// 遍历curA 和 curB,遇到相同则直接返回 | ||
while (curA != NULL) { | ||
if (curA == curB) { | ||
return curA; | ||
} | ||
curA = curA->next; | ||
curB = curB->next; | ||
} | ||
return NULL; | ||
} | ||
}; | ||
``` | ||
* 时间复杂度:$O(n + m)$ | ||
* 空间复杂度:$O(1)$ | ||
## 其他语言版本 | ||
Java: | ||
Python: | ||
Go: | ||
----------------------- | ||
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) | ||
* B站视频:[代码随想录](https://space.bilibili.com/525438321) | ||
* 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ) | ||
<div align="center"><img src=../pics/公众号.png width=450 alt=> </img></div> |