21. 合并两个有序链表


21. 合并两个有序链表

输入:l1 = [1,2,4], l2 = [1,3,4]
输出:[1,1,2,3,4,4]

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode() {}
 *     ListNode(int val) { this.val = val; }
 *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
 * }
 */
class Solution {
    public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
        if (l1 == null) {
            return l2;
        } else if (l2 == null) {
            return l1;
        &#125; else if (l1.val < l2.val) &#123;
            l1.next = mergeTwoLists(l1.next, l2);
            return l1;
        &#125; else &#123;
            l2.next = mergeTwoLists(l1, l2.next);
            return l2;
        &#125;

    &#125;

//作者:LeetCode-Solution
//链接:https://leetcode-cn.com/problems/merge-two-sorted-lists/solution/he-bing-liang-ge-you-xu-lian-biao-by-leetcode-solu/

&#125;

文章作者:   future
版权声明:   本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 future !
 上一篇
350. 两个数组的交集 II 350. 两个数组的交集 II
给定两个数组,编写一个函数来计算它们的交集。 示例 1:输入:nums1 = [1,2,2,1], nums2 = [2,2]输出:[2,2]示例 2:输入:nums1 = [4,9,5], nums2 = [9,4,9,8,4]输出:[4
2021-02-27 future
下一篇 
70.爬楼梯 70.爬楼梯
70. 爬楼梯 public int climbStairs(int n) &#123; int p = 1, q = 1, r = 1; for (int i = 2; i <= n; +
2021-02-27 future
  目录