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 ...
随机推荐
- 工作中Docker使用命令笔记
docker安装与启动 安装docker [root@localhost /]# yum -y install docker-io 更改配置文件 [root@localhost /]# vi /etc ...
- Debian9镜像安装问题
Debian9下载地址 https://www.debian.org/distrib/ Debian9有三个镜像文件 第一个包含系统2.3两个主要是一些软件的安装包只需下载第一个安装系统即可 默认安装 ...
- COGS 1570. [POJ3461]乌力波
★☆ 输入文件:oulipo.in 输出文件:oulipo.out 简单对比时间限制:1 s 内存限制:256 MB [题目描述] 法国作家乔治·佩雷克(Georges Perec,1 ...
- DAX:New and returning customers
The New and Returning Customers pattern dynamically calculates the number of customers with certain ...
- VBA Promming——入门教程
VBA Visual Basic for Applications(VBA)是Visual Basic的一种宏语言,是微软开发出来在其桌面应用程序中执行通用的自动化(OLE)任务的编程语言.主要能用来 ...
- 骑芯供应链(W 笔试)
单选题 1.调用本身构造方法 答案:this(X) 2.若y=3,a=2,b=4,运行y+=a++/--b,求y的值 答案: 3.不能控制servlet生命周期的方法是? 选项:init().serv ...
- uva12105 Bigger is Better
更简单的做法:定义状态dp[i][j]表示在已经用了i根火柴的情况下拼出来了剩余部分(是剩余部分,不是已经拼出来了的)为j(需要%m)的最大长度,一个辅助数组p[i][j]表示状态[i][j]的最高位 ...
- Mac 下用homebrew安装配置MongoDB
---恢复内容开始--- 1.首先安装homebrew,已有就跳过 /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent. ...
- docker nginx 负载均衡
index1.html <html> <head> <title>lb-test</title> </head> <body> ...
- devops issue
1.Nginx(refercence:https://zhuanlan.zhihu.com/p/24382606) summary: DJANGO_PROJECT = /home/django/dja ...