给定两个单链表,检测两个链表是否有交点,如果有返回第一个交点。
Node* Looknode(List p,List q)
{
assert(p != NULL);
assert(q != NULL);
if (p == NULL || q == NULL)
{
return NULL;
}
int Length_p = GetLength(p);
int Length_q = GetLength(q);
while (Length_p > Length_q)
{
p = p->next;
Length_p--;
}
while (Length_p < Length_q)
{
q = q->next;
Length_q--;
}
while (p != q&&p!=NULL&&q!=NULL)
{
p = p->next;
q = q->next;
}
if (p == NULL || q == NULL)
{
return NULL;
}
return p;
}