问题:已知两个按元素递增排列的链表,求二者交集,要求将结果放入第一个链表中。
/*
* Intersection of two ordered linked lists.
*
* fduan, Dec. 28, 2011.
*/
void intersect_v2( link_list * lst_a, link_list lst_b )
{
node_ptr pa = *lst_a, pb = lst_b->next, p = NULL;
while( pa->next != NULL && pb != NULL )
{
if( pa->next->data < pb->data )
{
p = pa->next;
pa->next = p->next;
free( p );
}
else if( pa->next->data > pb->data )
{
pb = pb->next;
}
else
{
pa = pa->next;
pb = pb->next;
}
}
while( pa->next != NULL )
{
p = pa->next;
pa->next = p->next;
free( p );
}
}