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 ...
随机推荐
- [转]qt QTableWidget&&QTableView 导出数据到excel
转自http://blog.csdn.net/fairystepwgl/article/details/54576372 注意:由于在qt导出的过程中分为QTableWidget导出文件到excel和 ...
- UVA 11346 Probability 概率 (连续概率)
题意:给出a和b,表示在直角坐标系上的x=[-a,a] 和 y=[-b,b]的这样一块矩形区域.给出一个数s,问在矩形内随机选择一个点p=(x,y),则(0.0)和p点组成的矩形面积大于s的概率是多少 ...
- core 中使用 nlog
引包 代码 public void Configure(IApplicationBuilder app, IHostingEnvironment env,ILoggerFactory logFac) ...
- 两个乒乓球队进行比赛,各出三人。 甲队为a,b,c三人,乙队为x,y,z三人。 已抽签决定比赛名单。 有人向队员打听比赛的名单。 a说他不和x比,c说他不和x,z比, 请编程序找出三队赛手的名单。
题目:两个乒乓球队进行比赛,各出三人. 甲队为a,b,c三人,乙队为x,y,z三人. 已抽签决定比赛名单. 有人向队员打听比赛的名单. a说他不和x比,c说他不和x,z比, 请编程序找出三队赛手的名单 ...
- 微信小程序工具真机调试提示page "xxx/xxx/xxx" is not found
解决方法: pages对象添加该页面
- TWaver可视化编辑器的前世今生(四)电力 云计算 数据中心
插播一则广告(长期有效) TWaver需要在武汉招JavaScript工程师若干 要求:对前端技术(JavasScript.HTML.CSS),对可视化技术(Canvas.WebGL)有浓厚的兴趣基础 ...
- python-opencv 分离图片(视频)中的某一颜色物体
看代码: import cv2 as cv import numpy as np def separate_color(frame): cv.imshow("原图", frame) ...
- 利用python库twilio来免费发送短信
大家好,我是四毛,最近开通了个人公众号“用Python来编程”,欢迎大家“关注”,这样您就可以收到优质的文章了. 今天跟大家分享的主题是利用python库twilio来免费发送短信. 先放一张成品图 ...
- OERR: ORA-1410 "invalid ROWID" Master Note / Troubleshooting, Diagnostic and Solution (文档ID 1410.1)
OERR: ORA-1410 "invalid ROWID" Master Note / Troubleshooting, Diagnostic and Solution (文档I ...
- c++_加法变乘法
加法变乘法 我们都知道:1+2+3+ ... + 49 = 1225现在要求你把其中两个不相邻的加号变成乘号,使得结果为2015 比如:1+2+3+...+10*11+12+...+27*28+29+ ...