关闭

Merge Two Sorted Lists

标签: java算法
157人阅读 评论(0) 收藏 举报
分类:

 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.

public class Solution {
    public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
        if(l1 == null){
            return l2;
        }
        if(l2 == null){
            return l1;
        }

        ListNode mergeHead;
        if(l1.val < l2.val){
            mergeHead = l1;
            mergeHead.next = mergeTwoLists(l1.next, l2);
        }
        else{
            mergeHead = l2;
            mergeHead.next = mergeTwoLists(l1, l2.next);
        }
        return mergeHead;
    }
}


0
0
查看评论

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 together the nodes of the first two lists. 题意:按顺序拼...
  • qq_28119401
  • qq_28119401
  • 2016-09-18 21:32
  • 635

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

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

【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 together the nodes ofthe first two lists. 融合两个有序的链表...
  • hqq39
  • hqq39
  • 2015-06-18 13:37
  • 427

[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
  • 3603

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

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

Merge Two Sorted Lists -- LeetCode

原题链接: http://oj.leetcode.com/problems/merge-two-sorted-lists/  这道题目比较简单,经典的链表基本操作。维护两个指针对应两个链表,因为一般会以一条链表为基准,比如说l1, 那么如果l1当期那的元素比较小,那么直接移动l1...
  • linhuanmars
  • linhuanmars
  • 2014-02-22 23:31
  • 16615

LeetCode | Merge Two Sorted Lists

题目: 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. ...
  • lanxu_yy
  • lanxu_yy
  • 2013-09-14 19:11
  • 6608

[C语言][LeetCode][21]Merge Two Sorted Lists

题目Merge Two Sorted Lists 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...
  • Timsley
  • Timsley
  • 2016-03-04 08:26
  • 836

LeetCode 21:Merge Two Sorted Lists

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 splicing toget...
  • sunao2002002
  • sunao2002002
  • 2015-05-27 23:08
  • 4038

【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 splicing together the nodes of the first two lists...
  • xiaoliucool1314
  • xiaoliucool1314
  • 2016-02-28 11:23
  • 235
    个人资料
    • 访问:103026次
    • 积分:3342
    • 等级:
    • 排名:第11969名
    • 原创:227篇
    • 转载:40篇
    • 译文:2篇
    • 评论:8条
    最新评论