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(链表)的更多相关文章

  1. [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 ...

  2. [LintCode] Intersection of Two Linked Lists 求两个链表的交点

    Write a program to find the node at which the intersection of two singly linked lists begins. Notice ...

  3. [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 ...

  4. 2016.5.24——Intersection of Two Linked Lists

    Intersection of Two Linked Lists 本题收获: 1.链表的输入输出 2.交叉链表:这个链表可以有交叉点,只要前一个节点的的->next相同即可. 题目:Inters ...

  5. 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 ...

  6. LeetCode--LinkedList--160. Intersection of Two Linked Lists(Easy)

    160. Intersection of Two Linked Lists(Easy) 题目地址https://leetcode.com/problems/intersection-of-two-li ...

  7. 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 ...

  8. 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 ...

  9. [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 ...

随机推荐

  1. JVM_Bind问题的解决方案

    心得:删除javaw.exe进程即可. 以下是网络的解决方案: JVM_Bind问题出现通常有两种情况. 一种是原来的javaw.exe没有结束掉而又新创建了一个javaw.exe进程.这本无可厚非, ...

  2. (转)Spring4.2.5+Hibernate4.3.11组合开发

    http://blog.csdn.net/yerenyuan_pku/article/details/52887573 搭建和配置Spring与Hibernate整合的环境 今天我们来学习Spring ...

  3. Android接入支付宝和微信支付

    然后把下载下来的aar包,放到项目目录下面的libs目录下,通过下面的gradle依赖进来 // 支付宝 SDK AAR 包所需的配置compile(name: 'alipaySdk-15.6.0-2 ...

  4. exportfs - 管理NFS共享文件系统列表

    概述 (SYNOPSIS) /usr/sbin/exportfs [-avi] [-o options,..] [client:/path ..] /usr/sbin/exportfs -r [-v] ...

  5. TIOJ1208 第K大连续和

    第k大的题一般都有点麻烦 pbds库的tree,需要研究一下https://codeforces.com/blog/entry/11080find_by_order() and order_of_ke ...

  6. 浅谈p值(p-value是什么)

    当我们说到p-value时,我们在说什么? “这个变量的p-value小于0.05,所以这个变量很重要” ........ 你真的知道自己在说什么么???这个p-value到底是个什么鬼?为什么小于0 ...

  7. SQL Sever中多列拼接成一列值为NULL

    查询出数据 SELECT a.ID AS KYMain_ID , ',' + a.Leader + ',' AS KYMain_Leader , ), b.TaskLeader) FROM TB_KY ...

  8. 安装Yii2提示Failed to decode response: zlib_decode(): data error错误解决方法

    如果是根据官方文档来安装(composer create-project --prefer-dist yiisoft/yii2-app-basic basic),并提示此错误的话,那么请做: 1. 请 ...

  9. 使用dockerfile构建nginx镜像

    使用dockerfile构建nginx镜像 docker构建镜像的方法:   commit.dockerfile 1.使用commit来构建镜像: commit是基于原有镜像基础上构建的镜像,使用此方 ...

  10. VMware Workstation 14 UEFI启动

    1.新建虚拟机 完成后不要启动 修改虚拟机目录下的XXX.vmx文件 添加一行:firmware="efi" 然后启动UEFI安装系统.