CNode* Intersection(CList head1, CList head2)//两个链表的第一个交点
{
assert(head1 != NULL && head2 != NULL);
if (head1 == NULL || head2 == NULL)
{
return NULL;
}
int i = 0, j = 0, k = 0;
CNode* p=head1;
CNode* q=head2;
p = (CNode*)malloc(sizeof(CNode));
q = (CNode*)malloc(sizeof(CNode));
assert(p != NULL && q != NULL);
while (p!= NULL&&p->next!=NULL)
{
i++;
p = p->next;
}
while (q!= NULL&&q->next!=NULL)
{
j++;
q = q->next;
}
if (i - j > 0)
{
for (k = 0; k < i - j; k++)
{
p = p->next;
}
}
else if (i - j < 0)
{
for (k = 0; k < j - i; k++)
{
q = q->next;
}
}
while (p!=NULL&&q!=NULL)
{
if (p == q)
{
break;
}
p = p->next;
q = q->next;
}
return p;
}
//*****