1.  
  2. /*
  3. 发现写长代码的时候写分成小部分去实现很有必要,容易差错
  4. */
  5. #include<stdio.h>  
  6. #include<stdlib.h>  
  7. struct node  
  8. {  
  9.     int num;  
  10.     struct node* next;  
  11.  
  12. };  
  13. struct node * input(int n)  
  14. {  
  15.     int i;  
  16.     struct node *head,*tail,*p;  
  17.     head = tail = (struct node *)malloc(sizeof(struct node));  
  18.     head->next = NULL;  
  19.  
  20.     for(i = 0; i < n; i++)  
  21.     {  
  22.         p = (struct node *)malloc(sizeof(struct node));  
  23.         p->next = NULL;  
  24.         scanf("%d", &p->num);  
  25.         tail->next = p;  
  26.         tail = tail->next;  
  27.     }  
  28.  
  29.     return head;  
  30. }  
  31.  
  32. int mix(struct node *head1,struct node *head2)  
  33. {  
  34.     struct node *tail,*p1,*p2;  
  35.     p1 = head1->next;  
  36.     p2 = head2->next;  
  37.     free(head2);  
  38.     tail = head1;  
  39.     while(p1&&p2)  
  40.     {  
  41.         if(p1->num < p2->num)  
  42.         {  
  43.             tail->next = p1;  //tail是游动指针
  44.             tail = p1;  //tail徐游动,就像建立新的链表一样
  45.             p1 = p1->next;  
  46.         }  
  47.         else 
  48.         {  
  49.             tail->next = p2;  
  50.             tail = p2;  
  51.             p2 = p2->next;  
  52.         }  
  53.     }  
  54.     if(p1)  
  55.     {  
  56.         tail->next = p1;  
  57.     }  
  58.     else 
  59.     {  
  60.         tail->next = p2;  
  61.     }  
  62.     p1 = head1->next;  
  63.     while(p1->next != NULL)  
  64.     {  
  65.         printf("%d ",p1->num);  
  66.         p1 = p1->next;  
  67.     }  
  68.     printf("%d",p1->num);  
  69.     return 0;  
  70. }  
  71.  
  72. int main()  
  73. {  
  74.     struct node *head1,*tail,*head2,*p;  
  75.     int n1,n2;  
  76.  
  77.     scanf("%d%d",&n1,&n2);  
  78.  
  79.     head1 = input(n1);  
  80.     head2 = input(n2);  
  81.     p = head1->next;  
  82.  
  83.     mix(head1,head2);  
  84. }