题目:
判断两个单链表是否相交
给出两个单链表的头指针,比如h1,h2,判断这两个链表是否相交。
为了简化问题,假设两个链表均不带环
问题扩展:
1.如果链表可能有环列。
2.如果需要求出两个链表相交的第一个节点列?
struct Node{
int data;
Node* next;
};
//if there is no cycle
int isJoinedSimple(Node* h1,Node* h2)
{
while (h1->next!=NULL)
h1=h1->next;
while (h2->next!=NULL)
h2=h2->next;
return h1==h2;
}
//if there could exists cycle
int isJoined(Node* h1,Node* h2)
{
Node* cylic1=textCylic(h1);
Node* cylic2=textCylic(h2);
if (cylic1+cylic2==0) return isJoinedSimple(h1,h2);
if (cylic1==0&&cylic2!=0||cylic1!=0&&cylic2==0)
return 0;
Node* p=cylic1;
while (1)
{
if (p==cylic2||p->next==cylic2)
return 1;
p=p->next->next;
cylic1=cylic1->next;
if (p==cylic1) return 0;
}
}
Node* testCylic(Node* h1)
{
Node* p1=h1,*p2=h1;
while (p2!=NULL&&p2->next!=NULL)
{
p1=p1->next;
p2=p2->next->next;
if (p1==p2)
return p1;
}
return NULL;
}