题目:设有两个元素递增的单链表(带头结点),编写算法将二者合并为按元素递减排列的链表L,要求利用原表的结点空间存放L。
/*
* fduan, Dec. 28, 2011.
*/
void calc_union_v2( link_list * lst_a, link_list * lst_b )
{
node_ptr pa = *lst_a, pb = *lst_b, p = NULL;
int e1, e2;
reverse_llist( lst_a );
reverse_llist( lst_b );
while( pa->next != NULL && pb->next != NULL )
{
e1 = pa->next->data;
e2 = pb->next->data;
if( e1 < e2 )
{
p = pb->next;
pb->next = p->next;
p->next = pa->next;
pa->next = p;
}
else if( e1 == e2 )
{
pb = pb->next;
}
pa = pa->next;
}
traverse_llist( *lst_a );
}