This code is intended to merge, two sorted, linked lists into a single sorted, linked list: it is supposed to take two lists as input and destructively modify them so that at the end, they form a single, sorted, linked list in memory.
What are bugs in the code?
Code:
typedef struct link {
char *data;
struct link *next;
} list;
list *merge(list *input1, list *input2) {
list *output = NULL, *tail, *next;
do {
if(strcmp(input1->data, input2->data) <= 0) {
next = input1;
input1 = input1->next;
} else {
next = input2;
input2 = input2->next;
}
if (output == NULL) {
output = tail = next;
} else {
tail->next = next;
tail = tail->next;
}
} while (input1 != NULL && input2 != NULL);
if (input1 != NULL) tail->next = input1;
else tail->next = input2;
return output;
}