合并有序链表

struct node
{
    int v;
    node* next;
};


node* union_list(node* h1, node* h2)
{
    if(!h1) return h2;
    if(!h2) return h1;
    node* cur = new node;
    node* t1 = h1, *t2 = h2;
    while(h1&&h2)
    {
        if(h1->v <= h2->v)
        {
            cur->next = h1;
            cur = cur->next;
            h1 = h1->next;
        }else{
            cur->next = h2;
            cur = cur->next;
            h2 = h2->next;
        }
    }
    if(!h1) cur->next = h2;
    else cur->next = h1;
    return t1->v <= t2->v ? t1 : t2; 
}


猜你喜欢

转载自blog.csdn.net/juiceda/article/details/7989220