合并两个有序链表

 1 /**
 2  * Definition for singly-linked list.
 3  * struct ListNode {
 4  *     int val;
 5  *     ListNode *next;
 6  *     ListNode(int x) : val(x), next(NULL) {} //初始化当前结点值为x,指针为空
 7  * };
 8  */
 9 class Solution {
10 public:
11     ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {
12         if(l1 == nullptr) return l2;
13         if(l2 == nullptr) return l1;
14         if(l1->val < l2->val){
15             l1->next = mergeTwoLists(l1->next,l2);
16             return l1;
17         }
18         else{
19             l2->next = mergeTwoLists(l1,l2->next);
20             return l2;
21         }
22     }
23 };

 

posted @ 2019-06-04 17:09  Austin_anheqiao  阅读(147)  评论(0编辑  收藏  举报