//value指向交叉结点,list1,list2为需要操作的两个单向链表
bool IsIntersect( node* list1 , node* list2 , node* &value )
{
value = NULL;
if( list1 == NULL || list2 == null )
{
return false;
}
node* temp1 = list1;
node* temp2 = list2;
int size1 = 0;
int size2 = 0;
while( temp1 )
{
temp1 = temp1->next;
size1++;
}
while( temp2 )
{
temp2 = temp2->next;
size2++;
}
if( temp1 == temp2 )
{
if( size1 > size2 )
{
while( size1 - size2 > 0 )
{
list1 = list1->next;
size1--;
}
}
if( size2 > size1 )
{
while( size2 - size1 > 0 )
{
list2 = list2->next;
size2--;
}
}
while( list1 != list2 )
{
list1 = list1->next;
list2 = list2->next;
}
value = list1;
return true;
}
else
{
return false;
}
}
练习:判断两个无环链表是否相交--C实现
最新推荐文章于 2019-02-28 12:33:38 发布