Intersection of Two Linked Lists(链表)
Write a program to find the node at which the intersection of two singly linked lists begins.
For example, the following two linked lists:
A: a1 → a2
↘
c1 → c2 → c3
↗
B: b1 → b2 → b3
begin to intersect at node c1. 题意:就是求取两个链表的交汇点,
要求:无交汇点返回NULL,否则返回交汇点的地址。
暴力思路:O(nm),固定A链表的第一个点,依次遍历B链表看是否有相同的点;接着固定A链表的第二个点,再依次遍历B链表,直到找到相同点为止。
hash解:时间复杂度O(n+m),空间O(n)或者O(m)
两指针解法:我们发现只要两个链表长度一样,就只需同时后移节点指针比较一个,若其中一个较长呢,其实处理一下,把两个链表变成一样长即可。
解法步骤:
1.求出两个链表的长度
2.若一样长,无需处理;若其中一个较长,则只需让较长的链表先走abs(lengthA-lengthB)步即可。
3.同时后移节点指针,直到找到交汇点。
代码:
class Solution {
private:
int getLength(ListNode* head){
if(head==NULL) return ;
ListNode* p=head;
int res=;
while (p->next!=NULL)
{
++res;p=p->next;
}
return res;
}
public:
ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {
if(headA==NULL||headB==NULL) return NULL;
int length_A=getLength(headA);
int length_B=getLength(headB);
ListNode* pA=headA;
ListNode* pB=headB;
int dis=;
if (length_A>length_B)
{
dis=length_A-length_B;
while (dis>)
{
--dis;
pA=pA->next;
}
}else if(length_A<length_B)
{
dis=length_B-length_A;
while (dis>)
{
--dis;
pB=pB->next;
}
}
while (pA!=NULL&&pB!=NULL&&pA!=pB)
{
pA=pA->next;
pB=pB->next;
}
if(pA==pB&&pA!=NULL) return pA;
else return NULL;
}
};
Intersection of Two Linked Lists(链表)的更多相关文章
- [LeetCode]160.Intersection of Two Linked Lists(2个链表的公共节点)
Intersection of Two Linked Lists Write a program to find the node at which the intersection of two s ...
- [LintCode] Intersection of Two Linked Lists 求两个链表的交点
Write a program to find the node at which the intersection of two singly linked lists begins. Notice ...
- [LeetCode] 160. Intersection of Two Linked Lists 解题思路
Write a program to find the node at which the intersection of two singly linked lists begins. For ex ...
- 2016.5.24——Intersection of Two Linked Lists
Intersection of Two Linked Lists 本题收获: 1.链表的输入输出 2.交叉链表:这个链表可以有交叉点,只要前一个节点的的->next相同即可. 题目:Inters ...
- 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--LinkedList--160. Intersection of Two Linked Lists(Easy)
160. Intersection of Two Linked Lists(Easy) 题目地址https://leetcode.com/problems/intersection-of-two-li ...
- 160. Intersection of Two Linked Lists【easy】
160. Intersection of Two Linked Lists[easy] Write a program to find the node at which the intersecti ...
- LeetCode_160. Intersection of Two Linked Lists
160. Intersection of Two Linked Lists Easy Write a program to find the node at which the intersectio ...
- [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 ...
随机推荐
- _ 下划线 vue mixins 混入 变量前有下划线 变量不起作用
_ 下划线 vue mixins 混入 变量前有下划线 变量不起作用
- 样式化复选框(Styling Checkbox)
原理:https://www.tuicool.com/articles/y67jee 表现:http://www.freejs.net/demo/381/index.html https://www. ...
- 三大框架所使用的UI框架
- Bootstrap 3 Glyphicons are not working
Bootstrap 3 Glyphicons are not working 解答1 Note to readers: be sure to read @user2261073's comment a ...
- xcode菜单栏
File 文件 Edit 编辑 View 视图 Navigate 导航 Editor 编辑 Product 产品 Window 窗口 Help 帮助 File 文件 New 新建 ...
- SqlSugar直接执行Sql
参考:http://www.codeisbug.com/Doc/8/1132 我的思路: 1.数据库中写好sql 2.用SqlSugar直接执行sql,获取DataTable的数据 3.DataTab ...
- codevs 2853 方格游戏--棋盘dp
方格游戏:http://codevs.cn/problem/2853/ 这和传纸条和noip方格取数这两个题有一定的相似性,当第一眼看到的时候我们就会想到设计$dp[i][j][k][l]$(i,j表 ...
- C++之类成员的访问权限详解(一)
概念解析 众所周知,面向对象编程语言的特征之一就是封装,不同编程语言对于封装提供的实现有所不同,但原理大体上是相同的.C++提供了三种不同程度的访问权限来实现,主要是通过public.private. ...
- 洛谷p1049 01背包
dp水之旅背包 题目描述 有一个箱子容量为V(正整数,0<=V<=20000),同时有n个物品(0<n<=30,每个物品有一个体积(正整数). 要求n个物品中,任取若干个装入箱 ...
- [模板] Miller-Rabin 素数测试
细节挺多的.. #include<iostream> #include<cstdlib> #include<cstdio> #include<ctime> ...