#include<stdio.h> typedef struct Node{ int data; struct Node* next; }Node; // 创建链表 Node* createList(){ Node* head = (Node*)malloc(sizeof(Node)); head->next = NULL; return head; } // 向链表末尾插入元素 void insert(Node* head, int data){ Node* newNode = (Node*)malloc(sizeof(Node)); newNode->data = data; newNode->next = NULL; Node* p = head; while(p->next != NULL){ p = p->next; } p->next = newNode; } // 合并两个链表 Node* mergeLists(Node* la, Node* lb){ Node* lc = la; Node* pa = la->next; Node* pb = lb->next; while(pa != NULL && pb != NULL){ lc->next = pa; lc = pa; pa = pa->next; lc->next = pb; lc = pb; pb = pb->next; } if(pa != NULL){ lc->next = pa; } if(pb != NULL){ lc->next = pb; } return la; } // 打印链表的元素 void printList(Node* head){ if(head->next == NULL){ printf("链表为空\n"); return; } Node* p = head->next; while(p != NULL){ printf("%d", p->data); p = p->next; } } int main(){ Node* la = createList(); Node* lb = createList(); int num; scanf("%d", &num); while(num != 1){ insert(la, num); scanf("%d", &num); } scanf("%d", &num); while(num != 1){ insert(lb, num); scanf("%d", &num); } Node* lc = mergeLists(la, lb); printList(lc); return 0; } [2024-06-17 15:01:02 | AI写代码神器 | 1020点数解答]