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.

Example:

Input: 1->2->4, 1->3->4
Output: 1->1->2->3->4->4 代码:
public class ListNode
{
public int val;
public ListNode next;
public ListNode(int x) { val = x; }
} static void Main(string[] args)
{
ListNode l11=new ListNode();
ListNode l12 = new ListNode();
ListNode l13 = new ListNode();
l11.next = l12;
l12.next = l13;
ListNode l21 = new ListNode();
ListNode l22 = new ListNode();
ListNode l23 = new ListNode();
l21.next = l22;
l22.next = l23;
var res=MergeTwoSortedLists(l11, l21);
while (res != null)
{
Console.Write(res.val);
res = res.next;
}
Console.ReadKey();
} private static ListNode MergeTwoSortedLists(ListNode l1, ListNode l2)
{
if (l1 == null) return l2;
if (l2 == null) return l1;
if (l1.val < l2.val)
{
l1.next = MergeTwoSortedLists(l1.next, l2);
return l1;
}
else
{
l2.next = MergeTwoSortedLists(l1, l2.next);
return l2;
}
}

解析:

输入:两个链表

输出:合并后的链表

思想

  三种情况分别讨论,并且用递归的思想。

时间复杂度:O(n)

 

C# 写 LeetCode easy #21 Merge Two Sorted Lists的更多相关文章

  1. [Leetcode][Python]21: Merge Two Sorted Lists

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 21: Merge Two Sorted Listshttps://oj.le ...

  2. Leetcode练习题21. Merge Two Sorted Lists

    题目描述(easy) Merge Two Sorted Lists Merge two sorted linked lists and return it as a new list. The new ...

  3. 【LeetCode】21. Merge Two Sorted Lists 合并两个有序链表

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:合并,有序链表,递归,迭代,题解,leetcode, 力 ...

  4. LeetCode Linked List Easy 21. Merge Two Sorted Lists

    Description Merge two sorted linked lists and return it as a new list. The new list should be made b ...

  5. 【一天一道LeetCode】#21. Merge Two Sorted Lists

    一天一道LeetCode系列 (一)题目 Merge two sorted linked lists and return it as a new list. The new list should ...

  6. 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 t ...

  7. 【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 splici ...

  8. 【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 ...

  9. LeetCode:21. Merge Two Sorted Lists(Easy)

    1. 原题链接 https://leetcode.com/problems/merge-two-sorted-lists/description/ 2. 题目要求 给出两个已经从小到大排序的链表ls1 ...

随机推荐

  1. 51nod 1681

    题目 神犇题解 这题挺神的..思路很巧妙 首先想到DFS序(毕竟是子树问题),这道题可以转化成:我们对于每一个节点的子树区间去看,两棵树同一节点的这个子树区间有多少个相同元素,设个数为x,那么这个点的 ...

  2. python再议装饰器

    装饰器实质还是一个函数,是对其他函数进行装饰的函数.装饰器函数接受被装饰函数的函数名,返回被装饰函数的函数名.对一个函数进行装饰有两个原则:一是不能修改被装饰函数的源代码:二是被装饰函数的调用方式不可 ...

  3. phalcon: 按年分表的model怎么建?table2017,table2018...相同名的分表模型怎么建

    phalcon: 按年分表的model怎么建?table2017,table2018...相同名的分表模型怎么建 场景:当前有一张表:Ntime,因为表太大了,考虑要分表: Ntime2017 Nti ...

  4. Jquery 取值,赋值学习总结

    <h2>获取和设置文本框值:</h2> <input type="button" value="赋值文件框" id="v ...

  5. Winform中的dataGridView添加自动编号

    1.Winform中的dataGridView添加自动编号:http://blog.csdn.net/ohyoyo2014/article/details/38346887 2.如何为datagrid ...

  6. Java钉钉开发_02_免登授权(身份验证)

    源码已上传GitHub: https://github.com/shirayner/DingTalk_Demo 一.本节要点 1.免登授权的流程 (1)签名校验 (2)获取code,并传到后台 (3) ...

  7. Post提交和Get提交的区别

    表单提交中get和post的区别 1. get: 把表单内各个字段均显示在URL中. post:把表单内各个字段和内容放在html的header内一起传递给action所指的url,用户看不到. 2. ...

  8. CCSpriteBatchNode CCSpriteFrameCache

    3.27 精灵集合类(CCSpriteBatchNode) //以纹理来创建一个精灵集合对象 static CCSpriteBatchNode* createWithTexture(CCTexture ...

  9. python suds 调用webservice 缓存

    在linux系统中 如果webservice更新了字段 suds调用有可能缓存以前的字段或方法,对新的字段报找不到类型 TypeNotFound,或者对 新加的方法找不到该方法的错误. 当更新或添加w ...

  10. 1045 Favorite Color Stripe (30)(30 分)

    Eva is trying to make her own color stripe out of a given one. She would like to keep only her favor ...