#include <stdio.h>
#include <stdlib.h>
typedef struct Node {
int data;
struct Node* next;
} Node;
Node* mergeLists(Node* LA, Node* LB) {
Node* result = NULL;
Node** tail = &result;
while (LA || LB) {
Node* newNode;
if (LA && (!LB || LA->data < LB->data)) {
newNode = LA;
LA = LA->next;
} else if (LB) {
newNode = LB;
LB = LB->next;
} else {
break;
}
*tail = newNode;
tail = &newNode->next;
}
if (LB) {
*tail = LB;
}
return result;
}
void printList(Node* head) {
while (head) {
printf("%d ", head->data);
head = head->next;
}
printf("\n");
}
int main() {
Node LA[] = {{1, &LA[1]}, {3, &LA[2]}, {5, NULL}};
Node LB[] = {{1, &LB[1]}, {2, NULL}};
Node* mergedList = mergeLists(LA, LB);
printList(mergedList);
return 0;
}