【LeetCode】Intersection of Two Linked Lists(相交链表)
这道题是LeetCode里的第160道题。
题目讲的:
编写一个程序,找到两个单链表相交的起始节点。
如下面的两个链表:
在节点 c1 开始相交。
示例 1:
输入:intersectVal = 8, listA = [4,1,8,4,5], listB = [5,0,1,8,4,5], skipA = 2, skipB = 3
输出: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 个节点。示例 2:
输入:intersectVal = 2, listA = [0,9,1,2,4], listB = [3,2,4], skipA = 3, skipB = 1
输出:Reference of the node with value = 2
输入解释:相交节点的值为 2 (注意,如果两个列表相交则不能为 0)。从各自的表头开始算起,链表 A 为 [0,9,1,2,4],链表 B 为 [3,2,4]。在 A 中,相交节点前有 3 个节点;在 B 中,相交节点前有 1 个节点。示例 3:
输入:intersectVal = 0, listA = [2,6,4], listB = [1,5], skipA = 3, skipB = 2
输出:null
输入解释:从各自的表头开始算起,链表 A 为 [2,6,4],链表 B 为 [1,5]。由于这两个链表不相交,所以 intersectVal 必须为 0,而 skipA 和 skipB 可以是任意值。
解释:这两个链表不相交,因此返回 null。注意:
- 如果两个链表没有交点,返回
null.- 在返回结果后,两个链表仍须保持原有的结构。
- 可假定整个链表结构中没有循环。
- 程序尽量满足 O(n) 时间复杂度,且仅用 O(1) 内存。
好久之前做的题了,今天算是填个坑,回忆一下。
首先排除链表成环的情况。
方法一(暴力法):
两层循环,取其中一个链表的一个节点,同时遍历另一个链表的所有节点,判断这两个节点是否相等,如此重复,只要相交了,就一定会有相等的地方。时间复杂度为(m*n)。
方法二:
我们可以把其中一个链表的所有节点地址信息存到数组中,然后把另一个链表的每一个节点地址信息遍历数组,若相等则跳出循环,说明相交。虽然这个方法和暴力法没有什么太大的区别,但是这里可以建立 hash 表,使用 hash 排序提高速度。
方法三:
两个链表相交,则有后面的节点是全部共用的,所以我们可以用双指针分别从两个链表的头部出发,一直走到尾部,判断尾部地址是否相等 (pa==pb) 就行了。
方法四:
因为知道相交的部分长度相等,所以我们可以用长的链表长度减去短的链表长度,得到的相差的值,然后让长的链表从头开始走过这个长度差,再两个链表同时走,直到两个链表的遍历指针相等。若是走到NULL,则说明链表不相交。
我给出的:
方法四:
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {
int lena=0,lenb=0;
if(headA==NULL||headB==NULL)
return NULL;
ListNode *pa=headA,*pb=headB;
while(pa!=NULL){
pa=pa->next;
lena++;
}
while(pb!=NULL){
pb=pb->next;
lenb++;
}
if(lena>lenb){
pa=headA;
for(int i=0;i<lena-lenb;i++)
pa=pa->next;
pb=headB;
}
else{
pb=headB;
for(int i=0;i<lenb-lena;i++)
pb=pb->next;
pa=headA;
}
while(pa!=pb){
pa=pa->next;
pb=pb->next;
}
return pa;
}
};
它反馈的:

我总结的:
在破坏链表结果的条件下,再说几种方法吧:
方法五:
首先保存第二个链表的头节点,然后第二个链表整个接到第一个链表的后面,从第二个链表的第一个节点往后遍历,如果能够回来,则说明两个链表相交。
方法六:
保存第二个链表的头节点,然后把第二个链表头尾相连,再从第一个链表开始走,如果能走到第二个链表的头节点,则相交。

然后,我们再来考虑成环的情况吧:
如果一个带环,一个不带环:则一定是没有交点的,因为如果相交,无论如何都是两个链表同时成环。
两个都带环:一种是交于环内,一种是交于环外。同样,先求环外的长度差 L,然后长的先走 L 次,再同时走并进行判断,如果这个时候相等了,则说明交于环外,否则交于环内或是不相交。交于环内的情况,根据上一步,我们的指针已经到了入环口。这时我们可以在声明一个指针,假设为 pc,然后 pc=pa,再然后环内遍历一次,如果有在 pc==pa 之前,有 pc==pb,则说明环内相交,否则不相交。
【LeetCode】Intersection of Two Linked Lists(相交链表)的更多相关文章
- [LeetCode] Intersection of Two Linked Lists 两链表是否相交
Write a program to find the node at which the intersection of two singly linked lists begins. For ex ...
- 160 Intersection of Two Linked Lists 相交链表
编写一个程序,找到两个单链表相交的起始节点.例如,下面的两个链表:A: a1 → a2 ↘ ...
- LeetCode: Intersection of Two Linked Lists 解题报告
Intersection of Two Linked Lists Write a program to find the node at which the intersection of two s ...
- [LeetCode] Intersection of Two Linked Lists 求两个链表的交点
Write a program to find the node at which the intersection of two singly linked lists begins. For ex ...
- LeetCode——Intersection of Two Linked Lists
Description: Write a program to find the node at which the intersection of two singly linked lists b ...
- LeetCode Intersection of Two Linked Lists
原题链接在这里:https://leetcode.com/problems/intersection-of-two-linked-lists/ 思路:1. 找到距离各自tail 相同距离的起始List ...
- Intersection of Two Linked Lists两链表找重合节点
Write a program to find the node at which the intersection of two singly linked lists begins. For ex ...
- Leetcode 160 Intersection of Two Linked Lists 单向链表
找出链表的交点, 如图所示的c1, 如果没有相交返回null. A: a1 → a2 ↘ ...
- LeetCode Intersection of Two Linked Lists (找交叉点)
题意: 给两个链表,他们的后部分可能有公共点.请返回第一个公共节点的地址?若不重叠就返回null. 思路: 用时间O(n)和空间O(1)的做法.此题数据弱有些弱. 方法(1)假设两个链表A和B,用两个 ...
随机推荐
- phpmyadmin 出现Table 'root.pma_table_uiprefs' doesn't exist
原文链接:http://zhidao.baidu.com/link?url=ugBKDds94yxWhh_IZ6rZWZYSd2nO555EZ1WMClXRrqL0aKLc-HPDrZVSKZyDaD ...
- mysql命令行执行时不输出列名(字段名)
-N 即可 如:mysql -N -e "select * from test" 摘自:http://blog.csdn.net/eroswang/article/details/ ...
- 按Home键切换到后台后会触发libGPUSupportMercury.dylib: gpus_ReturnNotPermittedKillClient导致crash
转自:http://www.eoeandroid.com/thread-251598-1-1.html 好像有很多朋友都碰到过这个问题,即在真机调试时,按hone键返回桌面,再回到app时,app会c ...
- 如何诊断 11.2 集群节点驱逐问题 (文档 ID 1674872.1)
适用于: Oracle Database - Enterprise Edition - 版本 11.2.0.1 到 11.2.0.2 [发行版 11.2]本文档所含信息适用于所有平台 用途 这篇文档提 ...
- UVA821 PageHopping (Floyd)
求所有点直接的平均最短距离,保存一下出现过的点,题目保证是所有点连通,Floyd求出最短路以后两个for统计一下. #include<bits/stdc++.h> using namesp ...
- HDOJ4550 卡片游戏 随便销毁内存的代价就是wa//string类的一些用法
思路 标记最小的最后的位置 放在第一位 标记位置之前按left值小的左方大的右方 标记位置之后按顺序放在最后 不多说先贴上销毁内存的wa代码 销毁内存的wa代码 #include<cstdio ...
- 高精度A+B
#include<stdio.h> #include<string.h> int main() { int lenth1,lenth2,n,i,j,k,s; scanf(&qu ...
- 自己开发一个APP需要多少钱
广州APP开发公司[启汇网络]经常遇到有开发定制APP软件需求的企业,通常第一句问的就是“开发一款APP需要多少钱”,在做完客户行业的市场调查后,再了解客... 广州APP开发公司[启汇网络]经常遇到 ...
- common-fileupload上传文件
文件上传在web应用中非常普遍,要在jsp环境中实现文件上传功能是非常容易的,因为网上有许多用java开发的文件上传组件,本文以commons-fileupload组件为例,为jsp应用添加文件上传功 ...
- javascript的trigger事件
<html> <head> <script type="text/javascript" src="/jquery/jquery.js&qu ...



