写个防笔试
1 #include<iostream> 2 3 struct node 4 { 5 node* pnext; 6 int value; 7 node():pnext(NULL){} 8 }; 9 10 node* ListMerge(node*,node*); 11 int main() 12 { 13 node* head1,*head2; 14 head1=new node(); 15 head1->value=1; 16 head1->pnext=new node(); 17 head1->pnext->value=3; 18 head1->pnext->pnext=new node(); 19 head1->pnext->pnext->value=5; 20 21 head2=new node(); 22 head2->value=0; 23 head2->pnext=new node(); 24 head2->pnext->value=2; 25 head2->pnext->pnext=new node(); 26 head2->pnext->pnext->value=8; 27 28 node* head3=ListMerge(head1,head2); 29 30 31 32 } 33 node* ListMerge(node* head1,node* head2) 34 { 35 node* h1=head1,*h2=head2; 36 node* th=NULL,*gth; 37 38 while(h1!=NULL&&h2!=NULL) 39 { 40 if(th==NULL) 41 { 42 gth=th=h1->value<=h2->value?h1:h2; 43 th==h1?h1=h1->pnext:h2=h2->pnext; 44 45 } 46 else 47 { 48 th->pnext=h1->value<=h2->value?h1:h2; 49 th->pnext==h1?h1=h1->pnext:h2=h2->pnext; 50 th=th->pnext; 51 } 52 } 53 54 while(h1!=NULL) 55 { 56 th->pnext=h1; 57 h1=h1->pnext; 58 } 59 while(h2!=NULL) 60 { 61 th->pnext=h2; 62 h2=h2->pnext; 63 } 64 return gth; 65 }