给定两个单链表,编写算法找出两个链表的公共结点。
LinkList Search_Common(LinkList L1,LinkList L2)
{
int len1=Length(L1),len2=Lengh(L2);//计算表长
LinkList longList,shortList;
int dist;
if(len1>len2)
{
longList = L1->next;
shortList = L2->next;
dist = len1-len2; //表长之差
}
else
{
longList = L2->next;
shortList = L1->next;
dist = len2-len1;
}
while(dist–)
longList = longList->next; //表长的链表先遍历到第dist个结点然后同步
while(longList!=NULL)
{
if(longList == shortList)
return longList;
else
{
longList=longList->next;
shortList=shortList->next
}
}
return NULL;
}
04-24
11-15
11-15