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.

题意:

  给两个可能相交的链表,求出其交点位置。

思路:

  1.将其中一条链表(B)的首位相连,问题转换为“求一个带环链表(A)的入环位置”。

  2.从链表(A)起始处利用快慢指针(p1、p2)遍历环,得到快慢指针相等的结点(p1 == p2)的位置。

  3.将p1指向链表(A)的起始处后,p1、p2同步走。

  4.走至 p1 == p2 的位置处,即为所求结点。

  ps:1.要判断可能不相交的情况 2.不能改动原本的数据结构(B的尾指针在返回前要置为0)

C++:

 /**
* 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) {
if(headA == || headB == )
return ; ListNode *phb = headB;
while(phb->next != )
phb = phb->next;
phb->next = headB; bool isloop = false;
ListNode *pha = headA;
while(pha != )
{
if(pha == headB)
{
isloop = true;
break;
}
pha = pha->next;
} if(!isloop)
{
phb->next = ;
return ;
} ListNode *p1 = headA->next;
ListNode *p2 = headA->next->next; while(p1 != p2)
{
p1 = p1->next;
p2 = p2->next->next;
} p1 = headA; while(p1 != p2)
{
p1 = p1->next;
p2 = p2->next;
} phb->next = ;
return p1;
}
};

【LeetCode 160】Intersection of Two Linked Lists的更多相关文章

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

  2. [LeetCode 题解]:Intersection of Two Linked Lists

    前言   [LeetCode 题解]系列传送门:  http://www.cnblogs.com/double-win/category/573499.html   1.题目描述 Suppose an ...

  3. 【leetcode】Intersection of Two Linked Lists

    题目简述: Write a program to find the node at which the intersection of two singly linked lists begins. ...

  4. 【leetcode】Intersection of Two Linked Lists(easy)

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

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

  6. LeetCode OJ:Intersection of Two Linked Lists(两个链表的插入)

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

  7. 【LeetCode】Intersection of Two Linked Lists(相交链表)

    这道题是LeetCode里的第160道题. 题目讲的: 编写一个程序,找到两个单链表相交的起始节点. 如下面的两个链表: 在节点 c1 开始相交. 示例 1: 输入:intersectVal = 8, ...

  8. 【leetcode❤python】Intersection of Two Arrays

    #-*- coding: UTF-8 -*- #求两个集合的交集class Solution(object):    def intersection(self, nums1, nums2):     ...

  9. LeetCode算法题-Intersection of Two Linked Lists(Java实现)

    这是悦乐书的第178次更新,第180篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第37题(顺位题号是160).编写程序以找到两个单链表交叉的节点.例如: 以下两个链表: ...

随机推荐

  1. Floodlight中的临时流表

    运行Floodlight,在Mininet中新建一个拓扑之后,并未添加相关的流表项,但是主机之间却可以相互通信.执行pingall操作,任意两个主机之间都能通.相当于没有任何路由表的路由器,它是怎么让 ...

  2. *[topcoder]BracketExpressions

    http://community.topcoder.com/stat?c=problem_statement&pm=13243 就是能否通过把字符串中的'X'替换成"()" ...

  3. windows下编译FreeSwitch

    FreeSWITCH的是一个跨平台的开源电话交换平台 windows版本:win7 64位的操作系统 [下载] 我下载的是release版本,下载的文件是freeswitch-1.4.20.zip,下 ...

  4. C++定义全局变量/常量几种方法的区别

    在讨论全局变量之前我们先要明白几个基本的概念: 1. 编译单元(模块):    在IDE开发工具大行其道的今天,对于编译的一些概念很多人已经不再清楚了,很多程序员最怕的就是处理连接错误(LINK ER ...

  5. Android模拟器问题:No system images installed for this target

    CPU/ABI选项无法选择,提示:No system images installed for this target,也就是没有适合的系统镜像 打开Android Manager SDK 下载完后重 ...

  6. nodejs搭配phantomjs highcharts后台生成图表

    简单分享一下,后台使用nodejs结合highcharts.phantomjs生成报表图片的方法.这主要应用在日报邮件. 主要参考以下资料: http://www.highcharts.com/com ...

  7. 常用的coco2d-x游戏开发工具(转)

    物理编辑工具Physics Editing ToolsMekanimo 网址:http://www.mekanimo.net/PhysicsBench 网址:http://www.cocos2d-ip ...

  8. Log4j具体使用实例

    首先,下载log4j.jar架包(网上很多,随便下载一个就可以了), 第一步:新建java项目,Testlog4j,再在src中建立com.Testlog4j包,再建一个testlog4j.java文 ...

  9. Msys 编译 VS2013 ffmpeg

    1.新版本msys2编译ffmpeg 的时候提示无法找到cl 重新安装了msys2及vs2013环境,msys2升级更新后,执行编译提示没有发现c编译器. 开始以为是新装的msys2_shell.ba ...

  10. C#调用java程序

    前言: 最近跟项目组的人合作一个项目,由于之前我用的是java写的一个与android通信的程序,现在另一个同事来编写界面程序,由于C#编写起来比较方便,而我又不想重新写之前java的那段代码,于是需 ...