解题思路:

分三种情况

1.两个链表都是无环链表,则使用指针p1,p2,分别遍历到两个链表尾,如果p1===p2,说明链表相交,否则不相交

2.两个链表有一个有环,另一个无环,那么这种情况链表肯定不相交,因为如果其中一个有环,另一个和它相交,另一个也肯定会有环

3.两个链表都有环,找到链表a和链表b的环中的两个节点p1,p2,用pa=p1,然后用pa=pa->next,直到pa===p1(意思是让pa在环内跑一圈),如果在跑的过程中发现pa === p2,说明两个链表的环是同一个,则相交,如果到达pa===p1,说明两个链表不相交

下面是代码

  1 <?php
  2     #判断链表是否相交
  3     #一共有三种情况
  4     #1.两个链表都是无环链表
  5     #2.一个是有环链表,另一个是无环链表,这种情况不可能相交
  6     #3.两个链表都是有环链表
  7
  8     class Node {
  9         public $data;
 10         public $next;
 11     }
 12
 13     #判断是否有环
 14     function test_circle($head) {
 15         $p1 = $head;
 16         $p2 = $head;
 17
 18         while ($p2 != null && $p2->next != null) {
 19             $p1 = $p1->next;
 20             $p2 = $p2->next->next;
 21
 22             #链表存在环,注意要用===
 23             if ($p1 === $p2) {
 24                 return $p1;
 25             }
 26         }
 27
 28         #链表无环
 29         return null;
 30     }
 31
 32     #判断两个无环链表是否相交
 33     function test_intersect_simple($head1, $head2) {
 34         $p1 = $head1;
 35         $p2 = $head2;
 36
 37         while ($p1->next != null) {
 38             $p1 = $p1->next;
 39         }
 40
 41         while ($p2->next != null) {
 42             $p2 = $p2->next;
 43         }
 44
 45         #如果两个链表相交,则他们末必然相等
 46         #此处要注意用三等号,因为要判断引用是否相同
 47         return $p1 === $p2 ? true : false;
 48     }
 49
 50     #判断两个链表是否相交
 51     function test_intersect($head1, $head2) {
 52         $cir1 = test_circle($head1);
 53         $cir2 = test_circle($head2);
 54
 55         #两个链表都无环
 56         if ($cir1 == null && $cir2 == null) {
 57             return test_intersect_simple($head1, $head2);
 58         }
 59
 60         #如果其中一个链表存在环另一个不存在环,则肯定不会相交
 61         if (($cir1 != null && $cir2 == null) || ($cir1 == null && $cir2 != null)) {
 62             return false;
 63         }
 64
 65         #两个链表都存在环,如果相交,则两个链表的环肯定是同一个
 66         #利用cir1在环内走一圈,如果没有遇到cir2,说明两个链表不相交
 67         $p = $cir1;
 68         while ($cir1 !== $cir2) {
 69             $cir1 = $cir1->next;
 70             if ($p === $cir1) {
 71                 return false;
 72             }
 73         }
 74         return true;
 75     }
 76
 77     #head1,head2无环且相交
 78     $head1 = new Node();
 79     $head2 = new Node();
 80     $n11 = new Node();
 81     $n12 = new Node();
 82     $n21 = new Node();
 83     $n22 = new Node();
 84     $n23 = new Node();
 85     $np1 = new Node();
 86     $np2 = new Node();
 87     $head1->next = $n11;
 88     $n11->next = $n12;
 89     $n12->next = $np1;
 90     $np1->next = $np2;
 91     $head2->next = $n21;
 92     $n21->next = $n22;
 93     $n22->next = $n23;
 94     $n23->next = $np1;
 95
 96     $t = test_intersect($head1, $head2);
 97     var_dump($t);
 98     echo "<br>";
 99
100     #head1,head2其中一个有环
101     $head1 = new Node();
102     $head2 = new Node();
103     $n11 = new Node();
104     $n12 = new Node();
105     $n21 = new Node();
106     $n22 = new Node();
107     $n23 = new Node();
108     $head1->next = $n11;
109     $n11->next = $n12;
110     $n12->next = $head1;
111     $head2->next = $n21;
112     $n21->next = $n22;
113     $n22->next = $n23;
114
115     $t = test_intersect($head1, $head2);
116     var_dump($t);
117     echo "<br>";
118
119     #head1,head2都有环且相交
120     $head1 = new Node();
121     $head2 = new Node();
122     $n11 = new Node();
123     $n12 = new Node();
124     $n21 = new Node();
125     $n22 = new Node();
126     $n23 = new Node();
127     $head1->next = $n11;
128     $n11->next = $n12;
129     $n12->next = $head1;
130     $head2->next = $n21;
131     $n21->next = $n22;
132     $n22->next = $n23;
133     $n23->next = $n12;
134
135     $t = test_intersect($head1, $head2);
136     var_dump($t);
137     echo "<br>";
138 ?>

bool(true) 
bool(false) 
bool(true)

PHP 判断链表是否相交相关推荐

  1. 《编程之美》3.6判断链表是否相交之扩展:链表找环方法证明

    先看看原题:<编程之美>3.6编程判断两个链表是否相交,原题假设两个链表不带环. 注:位于(*)符号之间的文字出自于:http://blog.csdn.net/v_july_v/artic ...

  2. 判断链表是否相交并找出交点

    问题概述 单链表定义如下: public class ListNode {int val;ListNode next;ListNode(int x) {val = x;next = null;}} 编 ...

  3. 7_2判断两个单链表是否相交,若相交,求出第一个交点

    转载请注明出处:http://www.cnblogs.com/wuzetiandaren/p/4251372.html 声明:现大部分文章为寻找问题时在网上相互转载,此博是为自己做个记录记录,方便自己 ...

  4. 链表问题11——两个单链表相交的系列问题(三):判断两个有环链表是否相交

    题目 判断两个有环链表是否相交,相交则返回第一个相交节点,否则返回null 在考虑此问题时,根据前面几篇文章的解法,我们已经得到了各自链表的入环节点,分别为loop1和loop2 思路 以下是问题三的 ...

  5. 编程之美:编程判断两个链表是否相交

    1.问题描述 给出两个单向链表的头指针,比如h1.h2,判断两个链表是否相交.编程之美为了简化问题,假设两个链表均不带环. 如下图: 2.分析与解法 解法一:直观法,先判断第一个链表的每个节点是否在第 ...

  6. 【编程题目】编程判断俩个链表是否相交 ☆

    第 7 题(链表) 微软亚院之编程判断俩个链表是否相交 给出俩个单向链表的头指针,比如 h1,h2,判断这俩个链表是否相交. 为了简化问题,我们假设俩个链表均不带环. 问题扩展: 1.如果链表可能有环 ...

  7. 3.6 判断两个链表是否相交

    判断两个链表是否相交,若相交则求其交点(指第一个相交的点). 思路1,利用计数法: 遍历链表1,将其节点的内存地址存入map或者hashmap内.然后遍历链表2,并查询map或者hashmap,判断链 ...

  8. 判断两个链表是否相交

    方法:获得两个链表的长度,获得长度的差值len,然后首先遍历较长的链表len次,然后再同时遍历两个链表,如果有相同部分,两个链表就相交,如果没有,则不相交,即没有公共部分. 代码: #include ...

  9. ]数据结构:单链表之判断两个链表是否相交及求交点(带环、不带环)

    1.判断两个链表是否相交,若相交,求交点.(假设链表不带环) 两个指针同时指向两个链表,分别依次往后遍历链表到最后一个节点,如指针的值相同(即节点地址相同),反之没有交点. int IsCross(N ...

最新文章

  1. 第1课第4.4节_Android硬件访问服务编写HAL代码
  2. 关于微信小程序登录授权
  3. qmail+rt+dns+apache
  4. md5碰撞Java_java现在MD5加密不安全了吗?
  5. SparkContext转化为JavaSparkContext
  6. Python批量添加库搜索路径
  7. 博客编辑工具ckeditor
  8. JS--Console.log()详解
  9. (60)FPGA比较器实现(function)
  10. 十问:BAT技术大牛的核心学习方法
  11. Xilinx平台SRIO介绍(六)SRIO收发测试
  12. C#实现的基于SMTP协议的E-MAIL电子邮件发送客户端软件
  13. css 缩小图片后,图片变模糊的解决办法
  14. node.js--尝试做一个crub
  15. Java对象转为Json格式的String
  16. java activeMQ消息的发送与接收
  17. Android系统换字体不root,小编吐血整理,超实用免ROOT 安卓手机换字体软件
  18. hadoop命令无法创建目录
  19. 百分之百还原《京东商城》项目实战1
  20. 用 Windows Media Center 免费看大片 (一)

热门文章

  1. python之旅【第二篇】
  2. 城市轨道交通站应急照明疏散指示系统设计
  3. 计算机匹配函数,匹配函数VLOOKUP使用方法
  4. Python逆向进阶:Web逆向私单
  5. 1.HTML5文件的基本结构
  6. Kali获取手机照片
  7. CSS3选择器-属性选择器
  8. 如何在Ubuntu上安装并使用Docker
  9. 使用TFS2010管理敏捷项目生命周期-系列指南5 TFS 故事墙(Story Wall)-看板(Dashboard)-Workbrench使用
  10. ③【Java 组】蓝桥杯省赛真题 [黄金连分数][马虎的算式]持续更新中...