lc 合并两个有序链表

链接:https://leetcode-cn.com/submissions/detail/72680216/

代码:

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {
        if(l1 == NULL) return l2;
        if(l2 == NULL) return l1;
        if(l1->val < l2->val) {
            l1->next = mergeTwoLists(l1->next, l2);
            return l1;
        }
        else {
            l2->next = mergeTwoLists(l1, l2->next);
            return l2;
        }
    }
};
View Code

思路:分析子问题,是否与原问题只有问题规模上的差别。

猜你喜欢

转载自www.cnblogs.com/FriskyPuppy/p/12940617.html