题目描述:

判断两个单链表是否相交?假设链表没有环。

假如链表有环呢?

1.  假如没有环

那么如果两个链表相交的话,必然最后的节点一定是同一个节点。所以只需要各自扫描一遍链表,找到最后一个节点,比较是否相同即可。

O ( M + N)

// version 1
// test whether two lists are intersected
// assume each list has no circle
bool IsIntersectedNoCircle(ListNode *lhs, ListNode *rhs)
{
if(lhs == NULL || rhs == NULL)
return false; ListNode *tail1(lhs);
for(; tail1->next; tail1 = tail1->next); ListNode *tail2(lhs);
for(; tail2->next; tail2 = tail2->next); return tail1 == tail2;
}

2. 可能有环

首先,如何判断是否有环?

采用快慢指针,一个指针一次走两步,一个指针一次走一步,如果链表有环的话,必然最后慢指针会赶上快指针。

复杂度同样是 O ( M + N)。

如果还要求求出 进入环的那个节点: 可以用两个指针,一个从链表head 出发,另一个从刚刚快慢指针的相遇点出发,速度相同。

可以证明,两指针必定在入口处第一次相遇。

// test whether list has a circle
// if true, return the enter node
// if not, return NULL
ListNode* HasCircle(ListNode *list)
{
if(list == NULL || list->next == NULL)
return NULL; // fast/slow pointer method
// if circled, slow pointer will catch up with fast one
ListNode *fast(list), *slow(list); while(fast)
{
fast = fast->next;
if(fast == NULL) return NULL;
fast = fast->next;
slow = slow->next; if(fast == slow)
break;
}
assert(fast == NULL || slow == fast); if(fast == NULL)
return NULL; // list definitely has a circle
// find the enter node
// fast and enter will meet at the enter node
ListNode *enter(list); while(enter != fast)
{
enter = enter->next;
fast = fast->next;
} return enter;
}

根据是否有环,可以分为以下三个 case:

case 1: 都没有环:已经分析

case 2: 都有环: 注意到如果相交,这两个环必定是一模一样的。所以,只要判断是否有一个节点在另一个环中即可。

case 3: 一个有环,一个没有环: 一定不相交

// version 2
// assume lists may have a circle inside
bool IsIntersectedCircle(ListNode *lhs, ListNode *rhs)
{
ListNode *enter1(NULL), *enter2(NULL); enter1 = HasCircle(lhs);
enter2 = HasCircle(rhs); // case 1: both lists have no circles
if(enter1 == NULL && enter2 == NULL)
{
return IsIntersectedNoCircle(lhs, rhs);
}
// case 2: each have a circle
else if(enter1 && enter2)
{
ListNode *node(enter1->next); while(node != enter1 && node != enter2){
node = node->next;
}
return node == enter2;
}
// case 3: one has a circle, while the other not
else
{
return false;
}
}

完整代码如下:

// copyright @ L.J.SHOU Feb.27, 2014
// test whether two lists are intersected
#include <iostream>
#include <cassert>
using namespace std; struct ListNode{
int val;
ListNode *next;
ListNode(int x)
:val(x), next(NULL){}
}; ListNode* HasCircle(ListNode*);
bool IsIntersectedCircle(ListNode*, ListNode*); // version 1
// test whether two lists are intersected
// assume each list has no circle
bool IsIntersectedNoCircle(ListNode *lhs, ListNode *rhs)
{
if(lhs == NULL || rhs == NULL)
return false; ListNode *tail1(lhs);
for(; tail1->next; tail1 = tail1->next); ListNode *tail2(lhs);
for(; tail2->next; tail2 = tail2->next); return tail1 == tail2;
} // version 2
// assume lists may have a circle inside
bool IsIntersectedCircle(ListNode *lhs, ListNode *rhs)
{
ListNode *enter1(NULL), *enter2(NULL); enter1 = HasCircle(lhs);
enter2 = HasCircle(rhs); // case 1: both lists have no circles
if(enter1 == NULL && enter2 == NULL)
{
return IsIntersectedNoCircle(lhs, rhs);
}
// case 2: each have a circle
else if(enter1 && enter2)
{
ListNode *node(enter1->next); while(node != enter1 && node != enter2){
node = node->next;
}
return node == enter2;
}
// case 3: one has a circle, while the other not
else
{
return false;
}
} // test whether list has a circle
// if true, return the enter node
// if not, return NULL
ListNode* HasCircle(ListNode *list)
{
if(list == NULL || list->next == NULL)
return NULL; // fast/slow pointer method
// if circled, slow pointer will catch up with fast one
ListNode *fast(list), *slow(list); while(fast)
{
fast = fast->next;
if(fast == NULL) return NULL;
fast = fast->next;
slow = slow->next; if(fast == slow)
break;
}
assert(fast == NULL || slow == fast); if(fast == NULL)
return NULL; // list definitely has a circle
// find the enter node
// fast and enter will meet at the enter node
ListNode *enter(list); while(enter != fast)
{
enter = enter->next;
fast = fast->next;
} return enter;
} // destroy list
ListNode* Destroy(ListNode *list)
{
ListNode *next(NULL); while(list)
{
next = list->next;
delete list;
list = next;
} return NULL;
} int main(void)
{
ListNode *list(NULL); // testing case 1: both have no circles
list = new ListNode(1);
list->next = new ListNode(2);
list->next->next = new ListNode(3); ListNode *list2 = list->next;
cout << IsIntersectedCircle(list, list2) << endl; // testing case 2: both have circles
list2 = new ListNode(1);
list2->next = new ListNode(2);
list2->next->next = list->next; list->next->next->next = list->next;
cout << IsIntersectedCircle(list, list2) << endl; list->next->next->next = NULL;
list2->next->next = NULL; // testing case 3: only one has a circle
list->next->next->next = list->next;
cout << IsIntersectedCircle(list, list2) << endl;
list->next->next->next = NULL; list = Destroy(list);
list2 = Destroy(list2); return 0;
}

Interview----判断两个链表是否相交?的更多相关文章

  1. Oracle判断两个时间段是否相交

    SQL中常常要判断两个时间段是否相交,该如何判断呢?比如两个时间段(S1,E1)和(S2,E2).我最先想到的是下面的方法一.方法一:(S1 BETWEEN S2 AND E2) OR (S2 BET ...

  2. IT公司100题-7-判断两个链表是否相交

    问题:有一个单链表,其中可能有一个环,也就是某个节点的next指向的是链表中在它之前的节点,这样在链表的尾部形成一环.1.如何判断一个链表是不是这类链表? 问题扩展:1.如果链表可能有环呢?2.如果需 ...

  3. C# 判断两条直线是否相交

    直接上代码,过程不复杂 /// <summary> /// 判断两条线是否相交 /// </summary> /// <param name="a"& ...

  4. PHP判断两个矩形是否相交

    <?php $s = is_rect_intersect(1,2,1,2,4,5,0,3); var_dump($s); /* 如果两个矩形相交,那么矩形A B的中心点和矩形的边长是有一定关系的 ...

  5. C# 判断两个矩形是否相交

    源代码 public bool JudgeRectangleIntersect(double RecAleftX, double RecAleftY, double RecArightX, doubl ...

  6. cocos2d-x 判断两条直线是否相交

    bool GraphicsUtil::linesCross(b2Vec2 v0, b2Vec2 v1, b2Vec2 t0, b2Vec2 t1, b2Vec2 &intersectionPo ...

  7. Cracking the Coding Interview:: 寻找有环链表的环路起始节点

    给定一个有环链表,实现一个算法返回环路的开头节点. 这个问题是由经典面试题-检测链表是否存在环路演变而来.这个问题也是编程之美的判断两个链表是否相交的扩展问题. 首先回顾一下编程之美的问题. 由于如果 ...

  8. 如何判断单链表是否存在环 & 判断两链表是否相交

    给定一个单链表,只给出头指针h: 1.如何判断是否存在环? 2.如何知道环的长度? 3.如何找出环的连接点在哪里? 4.带环链表的长度是多少? 解法: 1.对于问题1,使用追赶的方法,设定两个指针sl ...

  9. You can Solve a Geometry Problem too (hdu1086)几何,判断两线段相交

    You can Solve a Geometry Problem too Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/3276 ...

随机推荐

  1. url重写后发布出错问题

    iis7 配置urlrewriter重写失效的问题 在IIS7下,如果使用微软的 URLRewriter 重写控件则需要在WEB.CONFIG中配置以下信息 第一个配置: <configSect ...

  2. HDU----(4549)M斐波那契数列(小费马引理+快速矩阵幂)

    M斐波那契数列 Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)Total Sub ...

  3. uva----(10794) A Different Task

      A Different Task  The (Three peg) Tower of Hanoi problem is a popular one in computer science. Bri ...

  4. SQL Server数据库(SQL Sever语言 CRUD)

    使用SQL Sever语言进行数据库的操作 常用关键字identity 自增长primary key 主键unique 唯一键not null 非空references 外键(引用) 在使用查询操作数 ...

  5. Javascript 严格模式详解(转)

    一.概述 除了正常运行模式,ECMAscript 5添加了第二种运行模式:"严格模式"(strict mode).顾名思义,这种模式使得Javascript在更严格的条件下运行. ...

  6. word 转 PDF时报错

    利用微软自带的com组件,把word转化成Pdf,利用vs2012调试时没有问题,但是发布到IIS时出错,错误为: 检索 COM 类工厂中 CLSID 为 {} 的组件时失败,原因是出现以下错误: 8 ...

  7. CentOS hadoop启动错误 JAVA_HOME is not set and could not be found

    ... Starting namenodes on [] localhost: Error: JAVA_HOME is not set and could not be found. localhos ...

  8. 解析网络json数据,模拟美团界面显示。

    <?xml version="1.0" encoding="UTF-8"?> <RelativeLayout xmlns:android=&q ...

  9. MySql插入记录时判断

    我们在开发数据库相关的逻辑过程中, 经常检查表中是否已经存在这样的一条记录, 如果存在则更新或者不做操作, 如果没有存在记录,则需要插入一条新的记录. 这样的逻辑固然可以通过两条sql语句完成. SE ...

  10. display:flex

    元素在x方向走,元素y不一样[高度].可以用对齐.align-items. align-self 自身调节元素在x方向走,元素在x方向距离.justify-content .   元素在x方向走,x方 ...