#include <stdio.h> #include <stdlib.h> typedef struct ListNode { int val; struct ListNode* next; } ListNode; ListNode* createList() { ListNode* head = NULL, * tail = NULL; int val; while (scanf("%d", &val) != EOF) { ListNode* node = (ListNode*)malloc(sizeof(ListNode)); node->val = val; node->next = NULL; if (head == NULL) { head = node; tail = node; } else { tail->next = node; tail = tail->next; } if (getchar() == '\n') { break; } } return head; } ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) { ListNode dummy = {0, NULL}; ListNode* tail = &dummy; while (l1 && l2) { if (l1->val < l2->val) { tail->next = l1; l1 = l1->next; } else { tail->next = l2; l2 = l2->next; } tail = tail->next; } tail->next = l1 ? l1 : l2; return dummy.next; } void printList(ListNode* head) { while (head) { printf("%d ", head->val); head = head->next; } printf("\n"); } int main() { ListNode* l1 = createList(); ListNode* l2 = createList(); ListNode* l3 = mergeTwoLists(l1, l2); printList(l3); return 0; }