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 ...
随机推荐
- Luogu-1381 单词背诵
先将n个单词插入哈希表,记录左右端点,每次右端点往后移动,读入一个新的单词并记录下它的哈希值,如果这个单词之前没出现过那么更新\(ans\)和\(minl\),如果左端点的单词出现了不止一次则可以往右 ...
- python中字符串使用需要注意的地方
1. r''的使用 'r'是防止字符转义的 如果路径中出现'\t'的话 不加r的话\t就会被转义 而加了'r'之后'\t'就能保留原有的样子 2. u''的使用 引号之前加上字母u时,python会将 ...
- BZOJ 3890 [Usaco2015 Jan]Meeting Time:拓扑图dp
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=3890 题意: 给你一个有向图,n个点(n <= 100),m条边. 且所有的边都是从 ...
- leetcode 7 Reverse Integer(水题)
so easy,注意一下输入不爆int但是反转以后可能爆int. class Solution { public: int gao(int w){ ) ; else{ ; while(w--){ an ...
- 利用Python进行文章特征提取(二)
本篇blog是利用Python进行文章特征提取的续篇,主要介绍构建带TF-IDF权重的文章特征向量. In [1]: # 带TF-IDF权重的扩展词库 # 在第一篇文档里 主要是利用词库模型简单判断单 ...
- PS 滤镜— —挤压效果
clc; clear all; close all; addpath('E:\PhotoShop Algortihm\Image Processing\PS Algorithm'); I=imread ...
- Agc012_E Camel and Oases
传送门 题目大意 坐标轴上有$n$个坐标,第$i$个坐标是$x_i$,初始你有一个容量$V$,当两个给定的坐标距离不超过$V$时,你可以从一个坐标到达另一个坐标,同时你还可以令$V=\lfloor \ ...
- 【C/C++】scanf,printf 函数
摘自http://www.cplusplus.com 1. scanf 函数 int scanf ( const char * format, ... ); Parameters format C s ...
- RTSP协议分析(二)
以下是某省IPTV的RTSP协商过程: DESCRIBE rtsp://118.122.89.27:554/live/ch10083121594790060557.sdp?playtype=1& ...
- mongodb数据迁移的两种方式
环境说明:bbs数据采集的数据越来越多,目前是50G,每天大概以200W的数据量增长.而当前服务器1.2上面的空间不足,需要把数据迁移到空间足够大的1.3上面去 尝试了2种方式对数据进行迁移,一种是r ...