您的位置:首页 > 其它

算法习题8:判断俩个链表是否相交

2013-10-13 17:44 561 查看
微软亚院之编程判断俩个链表是否相交
给出俩个单向链表的头指针,比如h1,h2,判断这俩个链表是否相交。
为了简化问题,我们假设俩个链表均不带环。

问题扩展:
1.如果链表可能有环列?
2.如果需要求出俩个链表相交的第一个节点列?

同样参考:http://bbs.csdn.net/topics/350118968

--------------------------------------------------------------

首先分析下,判断两个链表是否相交?

假设相交了,那么根据链表特性,相交以后就汇成一个链表,所以后面的节点都相同,可是从h1-->交点和h2-->交点经过的节点数不一定相同,这就使得我们判定他们什么时候相交不一定能够很快找到,除非采用时间复杂度O(len1*len2)这种强奸式的遍历!

我们继续想,既然相交以后的链表都一样了,那他们的共同特点就是,结尾一致,所以我们只要遍历到结尾即可,时间复杂度降为O(max(len1,len2))

但是,新问题又来了,如果存在环呢?这个尾就不存在了。首先要判断是否存在环。。

所以这个问题被分解为

1、链表是否存在环

2、有环和无环怎么找出是否相交

3、这个相交点在哪里(这个办法现在看了大家都是采用 哈希表和一个定理,http://www.cnblogs.com/BeyondAnyTime/archive/2012/07/06/2580026.html 这篇文章给了说明,但是那个定理我还保持怀疑态度,)

方法1:

判断有无环,可以利用slow=1步 fast =2步的指针来移动,如果fast->null 则无环,如果fast 与 slow相遇,则有环

方法2:

我这里采用逆序,就是对原序列头指针改成尾指针,尾指针变成头指针,

revert(h1);revert(h2)

如果是有环的则 revertH1 == h1 && revertH2 == h2

无环则(revertH1 != h1 && revertH2 != h2)

我还没看到有人采用逆序,所以这里就用这种方法写个代码把,不过这方法还是找不到相交点具体位置。。

//============================================================================
// Name        : JudgeIntersect.cpp
// Author      : YLF
// Version     :
// Copyright   : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================

#include <iostream>
using namespace std;

struct Node{
int value;
Node* next;
};

Node* h1 = NULL;
Node* h2 = NULL;
Node* p1Cur = NULL;
Node* p2Cur = NULL;

Node* pIntersect = NULL;

void nextNode(Node* p1, Node* p2);
bool isIntersect(Node* revertH1,Node* revertH2,bool isLoop);
bool RevertLink(Node* &head);

int main() {

int a[7] = {1,2,3,7,8,9,1};
int b[8] = {3,5,7,1,7,8,9,1};
int i=0,j=0;
for(j=0;j<8;j++){
Node* p2 = new Node();
p2->value = b[j];
p2->next = NULL;

if(h2==NULL){
h2 = p2;
}else{
p2Cur->next = p2;
}
p2Cur = p2;
if(j==4)
pIntersect = p2;
if(j == 7)
p2->next = pIntersect;
}

for(i=0;i<3;i++){
Node* p1 = new Node();
p1->value = a[i];
p1->next = NULL;

if(h1==NULL){
h1 = p1;
}else{
p1Cur->next = p1;
}
p1Cur = p1;
}

p1Cur->next = pIntersect;
pIntersect = NULL;

//上面是先自动生成两个相交的链表

//先逆序,这里先对h1逆序,再对h2逆序,这个顺序很重要,如果二者相交,那么会发现
//revertH2 == h1
Node *revertH1 = h1;
Node *revertH2 = h2;
RevertLink(revertH1);
RevertLink(revertH2);
//接下来从逆序开始判断是否相等,以及相等点在哪?
if(revertH1 == h1 && revertH2 == h2){
//存在环
if(isIntersect(revertH1,revertH2,true))
cout<<"exists loop and intersect "<<endl;
else
cout<<"有环,不相交";
}
else if(revertH1 != h1 && revertH2 != h2){
//不存在环
if(isIntersect(revertH1,revertH2,false))
cout<<"not exists loop but intersect "<<endl;
else
cout<<"无环,不相交";
}else
cout<<"不相交";

return 0;
}

bool RevertLink(Node* &head){
if(head==NULL)
return false;
Node* p1 = head;
if(p1->next == NULL)
return true;
Node* p2 = p1->next;
if(p2->next == NULL){
p1->next = NULL;
p2->next = p1;
head = p2;
return true;
}
Node* p3 = p2->next;

if(p1 == head)
p1->next = NULL;
//start revert
while(p3){
p2->next = p1;
p1 = p2;
p2 = p3->next;
Node* temp = p2;
p2 = p3;
p3 = temp;
}
p2->next = p1;
head = p2;
return true;

}

bool isIntersect(Node* revertH1,Node* revertH2,bool isLoop){
if(!isLoop){
if(revertH2 == h1){
return true;
}
}else{
Node* p1 = revertH1;
Node* p2 = revertH2;
while(p1 != p2){
p1 = p1->next;
p2 = p2->next->next;
}
cout<<endl<<"int:"<<p1->value;
return true;
}
return false;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息