对于链表可分为有环和无环,对于两个链表的关系可分为相交和不相交
那么两个链表的相交分为哪几种情况呢?
可以简单的分为六种情况
1.两个无环且不相交
2.两个无环相交
3.一个有环不相交
4.两个有环不相交
5.两个有环相交,交点不在环上
6.两个有环相交,交点在环上
下面画一张图就可以清楚的看到这几种情况
这六种情况又可以分为三种情形考虑
第一种情形
把第一种和第二种情况放在一起分析(两个链表都无环)。用两重循环,若两个都遍历到尾,都没有结点相同,则不相交。
Node* meetNodeNoRing(Node* list1, Node* list2)//无环
{
assert(list1&&list2);
Node* cur = list2;
while (list1)
{
list2 = cur;
while (list2)
{
if (list1 == list2)
return list1;
else
list2 = list2->next;
}
list1 = list1->next;
}
return NULL;
}
第二种情形
第三种,只要证明出一个有环,一个无环,则两个必不相交
//判断一个链表是否有环
bool findRing(Node* list)//用快慢指针
{
if (list == NULL)
return false;
Node* fast = list;
Node* slow = list;
while (fast&& fast->next)
{
Node* next = fast->next;
slow = slow->next;
fast = next->next;
if (fast == slow)
return true;
}
return false;
}
第三种情形
第四种,第五种,第六种放在一起分析
先判断出两个都有环,求出环的切入点,若两个链表的切入点相同,则两个链表相交,且交点不在环上,然后求出链表的头到切入点的距离,哪个距离大就先往后移动两个距离的差值,再两个链表同时往后移动,相同的结点就是交点。若切入点不同,一个链表的切入点不动,另一个往后移动,若两个会相遇,则相交,且整个链表都是交点。对于都有环,除去上述两种情况,只剩下不相交这种情况。
Node* meetNodeRing(Node* list1, Node* list2)//有环,只有两个都有环时,才可能相交
{
assert(list1&&list2);
if (!findRing(list1) && findRing(list2))//一个有环,一个无环,两个必不相交
//1没有环,2有环
return NULL;
else if (findRing(list1) && !findRing(list2))
//1有环,2没有环
return NULL;
Node* meetNode1 = MeetNode(list1);
Node* meetNode2 = MeetNode(list2);
Node *Enter1 = RingEnter(list1, meetNode1);
Node *Enter2 = RingEnter(list2, meetNode2);
if (findRing(list1) && findRing(list2))
{
if (Enter1 == Enter2)//两个环相交,入口点相同,所以交点不在环上
{
int count1 = HeadToEnter(list1, RingEnter(list1, meetNode1));//求出链表头到切入点的距离
int count2 = HeadToEnter(list2, RingEnter(list1, meetNode2));
if (count1 > count2)
{
int count = count1 - count2;
while (count--)
{
list1 = list1->next;
}
while (list1 != list2)
{
list1 = list1->next;
list2 = list2->next;
}
}
else if (count1 < count2)
{
int count = count2 - count1;
while (count--)
{
list2 = list2->next;
}
while (list1 != list2)
{
list1 = list1->next;
list2 = list2->next;
}
}
else
{
while (list1 != list2)
{
list1 = list1->next;
list2 = list2->next;
}
}
return list1;
}
else//入口点不同,可能相交,也可能不相交
{
Node* next = Enter1->next;
while (Enter1 != next)
{
if (next == Enter2)//相等说明,两个链的入口点都在环上,整个环都是交点
return next;
next = next->next;
}
return NULL;//两个链有环 不相交
}
}
return NULL;
}
附上运行所需的整体的函数代码
Node* MeetNode(Node* list)//求相遇点
{
if (list == NULL)
return NULL;
Node* fast = list;
Node* slow = list;
while (fast&& fast->next)
{
Node* next = fast->next;
slow = slow->next;
fast = next->next;
next = fast->next;
if (fast == slow)
return fast;
}
return NULL;
}
bool findRing(Node* list)//用快慢指针,判断有没有环
{
if (list == NULL)
return false;
Node* fast = list;
Node* slow = list;
while (fast&& fast->next)
{
Node* next = fast->next;
slow = slow->next;
fast = next->next;
if (fast == slow)
return true;
}
return false;
}
#include<assert.h>
Node* RingEnter(Node*list, Node* meetNode)//求环的切入点
//时间复杂度为O(N)
{
assert(list&&meetNode);
while (list != meetNode)
{
list = list->next;
meetNode = meetNode->next;
}
return meetNode;
}
void Printf(Node* list)
{
if (list == NULL)
return;
while (list)
{
cout << list->data << " ";
list=list->next;
}
cout << endl;
}
Node* meetNodeNoRing(Node* list1, Node* list2)//无环
{
assert(list1&&list2);
Node* cur = list2;
while (list1)
{
list2 = cur;
while (list2)
{
if (list1 == list2)
return list1;
else
list2 = list2->next;
}
list1 = list1->next;
}
return NULL;
}
int HeadToEnter(Node* list, Node* enter)//链表头到环入口的距离
{
assert(list&&enter);
int count = 0;
while (list != enter)
{
count++;
list = list->next;
}
return count;
}
Node* meetNodeRing(Node* list1, Node* list2)//有环,只有两个都有环时,才可能相交
{
assert(list1&&list2);
if (!findRing(list1) && findRing(list2))//一个有环,一个无环,两个必不相交
//1没有环,2有环
return NULL;
else if (findRing(list1) && !findRing(list2))
//1有环,2没有环
return NULL;
Node* meetNode1 = MeetNode(list1);
Node* meetNode2 = MeetNode(list2);
Node *Enter1 = RingEnter(list1, meetNode1);
Node *Enter2 = RingEnter(list2, meetNode2);
if (findRing(list1) && findRing(list2))
{
if (Enter1 == Enter2)//两个环相交,入口点相同,所以交点不在环上
{
int count1 = HeadToEnter(list1, RingEnter(list1, meetNode1));
int count2 = HeadToEnter(list2, RingEnter(list1, meetNode2));
if (count1 > count2)
{
int count = count1 - count2;
while (count--)
{
list1 = list1->next;
}
while (list1 != list2)
{
list1 = list1->next;
list2 = list2->next;
}
}
else if (count1 < count2)
{
int count = count2 - count1;
while (count--)
{
list2 = list2->next;
}
while (list1 != list2)
{
list1 = list1->next;
list2 = list2->next;
}
}
else
{
while (list1 != list2)
{
list1 = list1->next;
list2 = list2->next;
}
}
return list1;
}
else//入口点不同,可能相交,也可能不相交
{
Node* next = Enter1->next;
while (Enter1 != next)
{
if (next == Enter2)//相等说明,两个链的入口点都在环上,整个环都是交点
return next;
next = next->next;
}
return NULL;//两个链有环 不相交
}
}
return NULL;
}
void deleteNoTail(Node* list,Node* cur)
{
assert(list&&cur);
while (list&&list->next != cur)
{
list = list->next;
}
list->next = cur->next;
}
Node* find(Node *list,int x)
{
while (list->data != x)
list = list->next;
return list;
}
void PrintTailToHead(Node* list)
{
stack<Node*> s;
while (list)
{
s.push(list);
list=list->next;
}
while (!s.empty())
{
cout << s.top()->data << " ";
s.pop();
}
cout << endl;
}