C# 写 LeetCode easy #21 Merge Two Sorted Lists
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的更多相关文章
- [Leetcode][Python]21: Merge Two Sorted Lists
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 21: Merge Two Sorted Listshttps://oj.le ...
- 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 ...
- 【LeetCode】21. Merge Two Sorted Lists 合并两个有序链表
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:合并,有序链表,递归,迭代,题解,leetcode, 力 ...
- 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 ...
- 【一天一道LeetCode】#21. Merge Two Sorted Lists
一天一道LeetCode系列 (一)题目 Merge two sorted linked lists and return it as a new list. The new list should ...
- 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 ...
- 【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 ...
- 【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 ...
- LeetCode:21. Merge Two Sorted Lists(Easy)
1. 原题链接 https://leetcode.com/problems/merge-two-sorted-lists/description/ 2. 题目要求 给出两个已经从小到大排序的链表ls1 ...
随机推荐
- 《python基础教程(第二版)》学习笔记 列表/元组(第2章)
<python基础教程(第二版)>学习笔记 列表/元组(第2章)序列中的下标从0开始x='ABC' ==> x[0]='A', x[1]='B', x[2]='C'负数索引从右边开始 ...
- html5 + css3 + jQuery + 响应式布局设计
1. [代码][HTML]代码 <!DOCTYPE html><html dir="ltr" lang="zh-CN">< ...
- webserver的编写中出现的问题
在webserver编写过程中,出现过问题.就是标签<input>编写过程中少了name属性,导致程序无法读到<form>提交的数据.
- Http请求和响应
Http请求和响应 Http协议对浏览器发出的Request格式以及对Web服务器发出的Response格式有具体的规定. 请求部分由三部分组成: Requset line:请求行,位于第一行 Req ...
- ES _source字段介绍——json文档,去掉的话无法更新部分文档,最重要的是无法reindex
摘自:https://es.xiaoleilu.com/070_Index_Mgmt/31_Metadata_source.html The _source field stores the JSON ...
- import module, from module import funtion区别
import module与from module import funtion区别: import module导入模块后你需要使用module.function()来调用一个函数 from mod ...
- python setuptools安装
执行# python setup.py install 时发生如下错误 Traceback (most recent call last): File "setup.py", li ...
- Poj 3903 Stock Exchange(LIS)
一.Description The world financial crisis is quite a subject. Some people are more relaxed while othe ...
- HDOJ1015(简单深搜)
Safecracker Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total ...
- ubuntu dd烧录镜像文件
df命令查看挂在的U盘和硬盘,ssd 右边可以看到路径 sudo dd if=cn_windows_10_multiple_editions_version_1703_updated_july_201 ...