数据结构题典012:链表求交集之二(ANSI C)

来源:互联网 发布:形势与政策 网络强国 编辑:程序博客网 时间:2024/05/19 22:47

问题:已知两个按元素递增排列的链表,求二者交集,要求将结果放入第一个链表中。

/* * 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 );}}

原创粉丝点击