# include <bits/stdc++.h> using namespace std; struct list_node{ int val; struct list_node * next; }; list_node * input_list(void) { int n, val; list_node * phead = new list_node(); list_node * cur_pnode = phead; scanf("%d", &n); for (int i = 1; i <= n; ++i) { scanf("%d", &val); if (i == 1) { cur_pnode->val = val; cur_pnode->next = NULL; } else { list_node * new_pnode = new list_node(); new_pnode->val = val; new_pnode->next = NULL; cur_pnode->next = new_pnode; cur_pnode = new_pnode; } } return phead; } list_node * merge_list(list_node * head1, list_node * head2) { //////在下面完成代码 if(head1 == NULL){ return head2; } if(head2 == NULL){ return head1; } list_node* newHead = NULL; list_node* newTail = NULL; list_node* p1 = head1; list_node* p2 = head2; while(p1 != NULL && p2 != NULL){ list_node* p1Next = p1->next; list_node* p2Next = p2->next; if(p1->val < p2->val){ if(newHead == NULL){ newHead = p1; newTail = newHead; newTail->next = NULL; }else{ newTail->next = p1; newTail = p1; newTail->next = NULL; } p1 = p1Next; }else if(p1->val > p2->val){ if(newHead == NULL){ newHead = p2; newTail = newHead; newTail->next = NULL; }else{ newTail->next = p2; newTail = p2; newTail->next = NULL; } p2 = p2Next; }else{ if(newHead == NULL){ newHead = p1; newTail = newHead; newTail->next = NULL; }else{ newTail->next = p1; newTail = p1; newTail->next = NULL; } newTail->next = p2; newTail = p2; newTail->next = NULL; p1 = p1Next; p2 = p2Next; } } while(p1 != NULL){ list_node* p1Next = p1->next; if(newHead == NULL){ newHead = p1; newTail = newHead; newTail->next = NULL; }else{ newTail->next = p1; newTail = p1; newTail->next = NULL; } p1 = p1Next; } while(p2 != NULL){ list_node* p2Next = p2->next; if(newHead == NULL){ newHead = p2; newTail = newHead; newTail->next = NULL; }else{ newTail->next = p2; newTail = p2; newTail->next = NULL; } p2 = p2Next; } return newHead; } void print_list(list_node * head) { while (head != NULL) { printf("%d ", head->val); head = head->next; } puts(""); } int main () { list_node * head1 = input_list(); list_node * head2 = input_list(); list_node * new_head = merge_list(head1, head2); print_list(new_head); return 0; }