Merge

Node* Merge(Node *h1, Node *h2)
{
    Node *new, *p1, *p2, *p3;
    if ((new = (Node*)malloc(sizeof(Node))) == NULL) {
        printf("fail to allocate memory\n");
        return NULL;
    }
    p1 = h1; p2 = h2; p3 = new;
    while (p1 != NULL && p2 != NULL) {
        if (p1->data <= p2->data) {
            p3->next = p1;
            p1 = p1->next;
        }
        else {
            p3->next = p2;
            p2 = p2->next;
        }
        p3 = p3->next;
    }
    if (p1 == NULL)
        p3->next = p2;
    else
        p3->next = p1;
 
    p3 = new->next;
    free(new);
    return p3;
}
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License