编程之美——判断两个链表是否相交
2012-12-14 17:27
211 查看
编程之美——判断两个链表是否相交
问题:给出两个单向链表的头指针,判断这两个链表是否相交,这两个链表可能有环。
分析:具体见《编程之美》
测试程序:
#include "iostream"
using namespace std;
struct Node
{
int data;
Node *next;
};
//判断是否有环,返回bool,如果有环,返回环里的节点。
bool isCircle(Node *head,Node* &circleNode,Node* &lastNode)
{
Node *fast=head->next;
Node *slow=head;
while(fast!=slow&&slow&&fast)
{
if(fast->next!=NULL)
fast=fast->next;
if(fast->next==NULL)
lastNode=fast;
if(slow->next==NULL)
lastNode=slow;
fast=fast->next;
slow=slow->next;
}
if(slow==fast&&slow&&fast)
{
circleNode=fast;
return true;
}
else
return false;
}
bool detect(Node *head1,Node *head2)
{
Node *circleNode1;
Node *circleNode2;
Node *lastNode1;
Node *lastNode2;
bool isCircle1=isCircle(head1,circleNode1,lastNode1);
bool isCircle2=isCircle(head2,circleNode2,lastNode2);
cout<<isCircle1<<endl;
cout<<isCircle2<<endl;
//一个有环,一个无环
if(isCircle1!=isCircle2)
return false;
//两个都无环,判断最后一个节点是否相等
else if(!isCircle1 && !isCircle2)
{
return lastNode1==lastNode2;
}
//两个都有环,判断环里的节点是否能到达另一个链表环里的节点
else
{
Node *temp=circleNode1;
while(temp!=circleNode2)
{
if(temp==circleNode2)
return true;
temp=temp->next;
}
return false;
}
return false;
}
int main()
{
Node *n1=new Node();
Node *n2=new Node();
Node *n3=new Node();
Node *n4=new Node();
n1->data=1;
n2->data=2;
n3->data=3;
n4->data=4;
n1->next=n2;
n2->next=n3;
n3->next=n4;
n4->next=NULL;
Node * n5 = new Node();
Node * n6 = new Node();
Node * n7 = new Node();
Node * n8 = new Node();
n5->data=1;
n6->data=2;
n7->data=3;
n8->data=4;
n5->next = n6;
n6->next = n7;
n7->next = n8;
n8->next = NULL;
if(detect(n1,n2))
cout<<"相交"<<endl;
else
cout<<"不相交"<<endl;
return 0;
}
问题:给出两个单向链表的头指针,判断这两个链表是否相交,这两个链表可能有环。
分析:具体见《编程之美》
测试程序:
#include "iostream"
using namespace std;
struct Node
{
int data;
Node *next;
};
//判断是否有环,返回bool,如果有环,返回环里的节点。
bool isCircle(Node *head,Node* &circleNode,Node* &lastNode)
{
Node *fast=head->next;
Node *slow=head;
while(fast!=slow&&slow&&fast)
{
if(fast->next!=NULL)
fast=fast->next;
if(fast->next==NULL)
lastNode=fast;
if(slow->next==NULL)
lastNode=slow;
fast=fast->next;
slow=slow->next;
}
if(slow==fast&&slow&&fast)
{
circleNode=fast;
return true;
}
else
return false;
}
bool detect(Node *head1,Node *head2)
{
Node *circleNode1;
Node *circleNode2;
Node *lastNode1;
Node *lastNode2;
bool isCircle1=isCircle(head1,circleNode1,lastNode1);
bool isCircle2=isCircle(head2,circleNode2,lastNode2);
cout<<isCircle1<<endl;
cout<<isCircle2<<endl;
//一个有环,一个无环
if(isCircle1!=isCircle2)
return false;
//两个都无环,判断最后一个节点是否相等
else if(!isCircle1 && !isCircle2)
{
return lastNode1==lastNode2;
}
//两个都有环,判断环里的节点是否能到达另一个链表环里的节点
else
{
Node *temp=circleNode1;
while(temp!=circleNode2)
{
if(temp==circleNode2)
return true;
temp=temp->next;
}
return false;
}
return false;
}
int main()
{
Node *n1=new Node();
Node *n2=new Node();
Node *n3=new Node();
Node *n4=new Node();
n1->data=1;
n2->data=2;
n3->data=3;
n4->data=4;
n1->next=n2;
n2->next=n3;
n3->next=n4;
n4->next=NULL;
Node * n5 = new Node();
Node * n6 = new Node();
Node * n7 = new Node();
Node * n8 = new Node();
n5->data=1;
n6->data=2;
n7->data=3;
n8->data=4;
n5->next = n6;
n6->next = n7;
n7->next = n8;
n8->next = NULL;
if(detect(n1,n2))
cout<<"相交"<<endl;
else
cout<<"不相交"<<endl;
return 0;
}
相关文章推荐
- 编程之美:编程判断两个链表是否相交
- 《编程之美》——编程判断两个链表是否相交
- 编程之美--判断两个链表是否相交
- 编程之美3.6——编程判断两个链表是否相交
- 编程之美3.6——编程判断两个链表是否相交
- 判断两个链表是否相交(编程之美3.6)
- 读《编程之美》有感—判断两个链表是否相交
- 编程之美:编程判断两个链表是否相交
- 小算法系列-判断单链表是否存在环,判断两个链表是否相交问题详解(转)
- 《编程之美》3.6判断链表是否相交之扩展:链表找环方法证明
- 判断两个链表是否相交并找出交点-笔记
- 判断两个链表是否相交 【微软面试100题 第七题】
- 判断单链表是否存在环以及两个链表是否相交
- 编程判断两个链表是否相交 结构之法 6
- 判断两个链表是否相交并找出交点
- 链表--1.判断两个链表是否相交,若相交,求交点。(假设链表不带环)2.判断两个链表是否相交,若相交,求交点。(假设链表可能带环)
- 判断链表是否有环,找链表中间节点,判断两个链表是否相交
- 题目:①判断一个单向链表是否有环,如果有环则找到环的入口节点。 ②判断两个单向链表是否相交,如果相交则找到交点节点。
- 判断两个单向链表是否相交
- 判断两个链表是否相交,若相交,求交点。