#include<stdio.h>#include<stdlib.h>#include<assert.h>structListnode{int_value;Listnode*_next;};voidInit(Listnode*&head){Listnode*cur=head;if(cur==NULL){cur=(Listnode*)malloc(sizeof(Listnode));cur->_next=NULL;cur->_value=0;}head=cur;}voidpush(Listnode*&head,intvalue){Listnode*cur=head;while(cur->_next){cur=cur->_next;}Listnode*tmp=NULL;tmp=(Listnode*)malloc(sizeof(Listnode));tmp->_next=NULL;tmp->_value=value;cur->_next=tmp;}voidpop(Listnode*head){Listnode*cur=head;Listnode*prev=NULL;while(cur->_next!=NULL){prev=cur;cur=cur->_next;}prev->_next=NULL;free(cur);cur=NULL;}voidprint(Listnode*head){Listnode*cur=head;while(cur){printf("%d\n",cur->_value);cur=cur->_next;}}Listnode*reverse(Listnode*head){Listnode*newhead=NULL;if(head==NULL||head->_next==NULL){returnhead;}while(head){Listnode*tmp=head;head=head->_next;tmp->_next=newhead;newhead=tmp;}returnnewhead;}Listnode*merge(Listnode*head1,Listnode*head2){if(head1==NULL&&head2!=NULL){returnhead2;}elseif(head2==NULL&&head1!=NULL){returnhead1;}elseif(head1==NULL&&head2==NULL){returnNULL;}Listnode*newhead=NULL;Init(newhead);head1=head1->_next;head2=head2->_next;Listnode*cur=newhead;while(head1&&head2){if(head1->_value>head2->_value){cur->_next=head2;cur=cur->_next;head2=head2->_next;}else{cur->_next=head1;cur=cur->_next;head1=head1->_next;}}if(head1==NULL&&head2!=NULL){cur->_next=head2;cur=cur->_next;}elseif(head2==NULL&&head1!=NULL){cur->_next=head1;cur=cur->_next;}returnnewhead;}voidtest(){Listnode*head=NULL;Init(head);push(head,1);push(head,3);push(head,5);push(head,7);push(head,9);/*pop(head);*/Listnode*head2=NULL;Init(head2);push(head2,0);push(head2,2);push(head2,4);push(head2,6);push(head2,8);push(head2,10);Listnode*newhead=merge(head,head2);print(newhead);}intmain(){test();system("pause");return0;}

结果: