合并两个有序链表

 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     if (l2 == NULL) 
14         return l1;
15     struct ListNode *l=NULL;
16     if (l1->val < l2->val) {
17         l = l1;
18         l->next = mergeTwoLists(l1->next, l2);
19     } else {
20         l = l2;
21         l->next = mergeTwoLists(l1, l2->next);
22     }
23     return l;
24 }

 

posted @ 2020-09-29 11:30  yushimeng  阅读(130)  评论(0编辑  收藏  举报