#include <stdio.h>
#include <stdlib.h>

typedef struct node {
    int d;
    struct node *l, *r;
} *N;

void concat(N *X1, N *X2) {
    if (*X1 == NULL) *X1 = *X2;
    else if (*X2) {
        N t = *X1;
        while (t->r) t = t->r;
        t->r = *X2;
        (*X2)->l = t;
    }
}

N new_node(int d) {
    N n = (N)malloc(sizeof(struct node));
    n->d = d;
    n->l = n->r = NULL;
    return n;
}

void insert(N *h, int d) {
    N n = new_node(d);
    if (!*h) *h = n;
    else {
        N t = *h;
        while (t->r) t = t->r;
        t->r = n;
        n->l = t;
    }
}

void print(N h) {
    while (h) {
        printf("%d ", h->d);
        h = h->r;
    }
    printf("\n");
}

int main() {
    N X1 = NULL, X2 = NULL;
    insert(&X1, 1);
    insert(&X1, 2);
    insert(&X1, 3);
    insert(&X2, 4);
    insert(&X2, 5);
    concat(&X1, &X2);
    print(X1);
    return 0;
}
