LeetCode 21. Merge Two Sorted Lists

原创 2018年04月17日 21:49:26

问题描述

  • Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists.
  • 地址

问题分析

  • dummy
  • 递归

代码实现

  • dummy
    public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
        if(l1 == null || l2 == null) {
            return l1 == null ? l2 : l1;
        }
        ListNode dummy = new ListNode(0);
        ListNode tail = dummy;
        while (l1 != null && l2 != null) {
            if (l1.val < l2.val) {
                tail.next = l1;
                l1 = l1.next;
            }else {
                tail.next = l2;
                l2 = l2.next;
            }
            tail = tail.next;
        }
        tail.next = l1 != null ? l1 : l2;
        return dummy.next;
    }
  • 递归
     public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
        if(l1 == null || l2 == null) {
            return l1 == null ? l2 : l1;
        }
        if (l1.val > l2.val) {
            l2.next = mergeTwoLists(l1, l2.next);
            return l2;
        }else {
            l1.next = mergeTwoLists(l1.next, l2);
            return l1;
        }
     }
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/zjxxyz123/article/details/79981473

Leetcode 21. Merge Two Sorted Lists(C++)

问题: Merge Two Sorted Lists (将两个有序单链表合成一个有序的单链表) 参考链接: 1、[LeetCode]21.Merge Two Sorted Lists...
  • sqiu_11
  • sqiu_11
  • 2017-04-24 15:02:29
  • 526

【leetcode c++】21 Merge Two Sorted Lists

Merge two sorted linked lists and return itas a new list. The new list should be made by splicing to...
  • hqq39
  • hqq39
  • 2015-06-18 13:37:28
  • 491

21. Merge Two Sorted Lists(python)

Merge two sorted linked lists and return it as a new list. The new list should be made by splicing t...
  • qq_28119401
  • qq_28119401
  • 2016-09-18 21:32:17
  • 764

LeetCode 21:Merge Two Sorted Lists

LeetCode 21: Merge Two Sorted Lists   Merge two sorted linked lists and return it as a new list. ...
  • sunao2002002
  • sunao2002002
  • 2015-05-27 23:08:48
  • 4120

[leetcode-排序]--21. Merge Two Sorted Lists

Question 21. Merge Two Sorted Lists Merge two sorted linked lists and return it as a new list. The...
  • u010853261
  • u010853261
  • 2017-02-05 16:53:38
  • 905

[LeetCode] 021. Merge Two Sorted Lists (Easy) (C++/Python)

[LeetCode] 021. Merge Two Sorted Lists (Easy) (C++/Python)
  • hcbbt
  • hcbbt
  • 2015-03-04 20:11:32
  • 3757

LeetCode 21 Merge Two Sorted Lists(合并两个已排序的链表)(Linked List)

翻译合并两个排好序的链表,并返回这个新链表。 新链表应该由这两个链表的头部拼接而成。原文Merge two sorted linked lists and return it as a new lis...
  • NoMasp
  • NoMasp
  • 2015-11-10 20:38:04
  • 8927

Merge Two Sorted Lists -- LeetCode

原题链接: http://oj.leetcode.com/problems/merge-two-sorted-lists/  这道题目比较简单,经典的链表基本操作。维护两个指针对应两个链表,因为一般会...
  • linhuanmars
  • linhuanmars
  • 2014-02-22 23:31:00
  • 16906

leetcode第21题——*Merge Two Sorted Lists

题目 Merge two sorted linked lists and return it as a new list. The new list should be made by splicin...
  • buptlrw
  • buptlrw
  • 2016-02-28 10:09:15
  • 636

LeetCode(21)Merge Two Sorted Lists

题目如下: Merge two sorted linked lists and return it as a new list. The new list should be made by spli...
  • feliciafay
  • feliciafay
  • 2014-01-25 15:53:38
  • 973
收藏助手
不良信息举报
您举报文章:LeetCode 21. Merge Two Sorted Lists
举报原因:
原因补充:

(最多只允许输入30个字)