lucky&girl

导航

合并两个有序链表(C语言)

合并两个有序链表

将两个有序链表合并为一个新的有序链表并返回。新链表是通过拼接给定的两个链表的所有节点组成的。 

输入:1->2->4, 1->3->4
输出:1->1->2->3->4->4

分析:两个链表为有序链表,所以依次遍历两个链表比较大小即可。

        下边是代码实现:

  1 /**
  2  * Definition for singly-linked list.
  3  * struct ListNode {
  4  *     int val;
  5  *     struct ListNode *next;
  6  * };
  7  */
  8 
  9 
 10 struct ListNode* mergeTwoLists(struct ListNode* l1, struct ListNode* l2){
 11     if(l1==NULL){
 12         return l2;
 13     }
 14     if(l2==NULL){
 15         return l1;
 16     }
 17     struct ListNode *l = (struct ListNode*)malloc(sizeof(struct ListNode));
 18     l->next = NULL;
 19     struct ListNode *list1 = l1;
 20     struct ListNode *list2 = l2;
 21     if(l1->val<l2->val){
 22         l->val=l1->val;
 23         if(list1->next==NULL){
 24             l->next=list2;
 25             return l;
 26         }
 27         list1=list1->next;
 28     }else{
 29         l->val=l2->val;
 30         if(list2->next==NULL){
 31             l->next=list1;
 32             return l;
 33         }
 34         list2=list2->next;
 35     }
 36     struct ListNode *list = l;
 37     while(list1->next!=NULL&&list2->next!=NULL){
 38         if(list1->val<=list2->val){
 39             struct ListNode *body = (struct ListNode *)malloc(sizeof(struct ListNode));
 40             body->val = list1->val;
 41             body->next = NULL;
 42             list->next = body;
 43             list = list->next;
 44             list1 = list1->next;
 45         }else{
 46             struct ListNode *body = (struct ListNode*)malloc(sizeof(struct ListNode));
 47             body->val=list2->val;
 48             body->next=NULL;
 49             list->next=body;
 50             list=list->next;
 51             list2=list2->next;
 52         }
 53     }
 54     if(list1->next==NULL){
 55         while(list2->next!=NULL){
 56             if(list1->val<=list2->val){
 57                 list->next = list1;
 58                 list = list->next;
 59                 list->next=list2;
 60                 return l;
 61             }else{
 62                 struct ListNode *body = (struct ListNode*)malloc(sizeof(struct ListNode));
 63                 body->val=list2->val;
 64                 body->next=NULL;
 65                 list->next=body;
 66                 list=list->next;
 67                 list2=list2->next;
 68             }
 69         }
 70     }else{
 71         while(list1->next!=NULL){
 72             if(list2->val<=list1->val){
 73                 list->next=list2;
 74                 list=list->next;
 75                 list->next=list1;
 76                 return l;
 77             }else{
 78                 struct ListNode *body = (struct ListNode*)malloc(sizeof(struct ListNode));
 79                 body->val=list1->val;
 80                 body->next=NULL;
 81                 list->next=body;
 82                 list=list->next;
 83                 list1=list1->next;
 84             }
 85         }
 86     }
 87     if(list1->next==NULL&&list2->next==NULL){
 88         if(list1->val<=list2->val){
 89             list->next = list1;
 90             list=list->next;
 91             list->next=list2;
 92         }else{
 93             list->next=list2;
 94             list=list->next;
 95             list->next=list1;
 96         }
 97     }
 98     
 99     
100     
101     return l;
102 }

 

posted on 2019-09-06 14:17  lucky&girl  阅读(5367)  评论(0编辑  收藏  举报