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.
合并两个有序的链表并将其作为新链表返回。 新链表应通过将前两个列表的节点拼接在一起。
public ListNode mergeTwoLists(ListNode l1,ListNode l2){
ListNode newNode; //new一个新的节点作新的链表
if (l1 == null && l2 == null) {
return null;
}
if (l1 == null) { //如果L1为空,即L2长度大于L1,所以返回L2剩余节点
newNode = l2;
return newNode;
}
if (l2 == null) { //与上同理
newNode = l1;
return newNode;
}
if (l1.val > l2.val) {
newNode = l2;
l2 = l2.next;
} else {
newNode = l1;
l1 = l1.next;
}
newNode.next = mergeTwoLists(l1, l2); //采取递归思想
return newNode;
}
LeetCode记录之21——Merge Two Sorted Lists的更多相关文章
- leetCode练题——21. Merge Two Sorted Lists(照搬大神做法)
1.题目 21. Merge Two Sorted Lists Merge two sorted linked lists and return it as a new list. The new l ...
- [LeetCode&Python] Problem 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❤python】21. Merge Two Sorted Lists
#-*- coding: UTF-8 -*- # Definition for singly-linked list.# class ListNode(object):# def __init ...
- [Leetcode][Python]21: Merge Two Sorted Lists
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 21: Merge Two Sorted Listshttps://oj.le ...
- 21. Merge Two Sorted Lists(合并2个有序链表)
21. Merge Two Sorted Lists Merge two sorted linked lists and return it as a new list. The new list s ...
- 21. Merge Two Sorted Lists【easy】
21. Merge Two Sorted Lists[easy] Merge two sorted linked lists and return it as a new list. The new ...
- 21.Merge Two Sorted Lists 、23. Merge k Sorted Lists
21.Merge Two Sorted Lists 初始化一个指针作为开头,然后返回这个指针的next class Solution { public: ListNode* mergeTwoLists ...
- 刷题21. Merge Two Sorted Lists
一.题目说明 这个题目是21. Merge Two Sorted Lists,归并2个已排序的列表.难度是Easy! 二.我的解答 既然是简单的题目,应该一次搞定.确实1次就搞定了,但是性能太差: R ...
- [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 ...
随机推荐
- SQL SERVER FOR XML PATH合并字符串
两种方式,效率立竿见影 ------------------------------------------------ SET STATISTICS TIME ON DECLARE @OrderSt ...
- 408. Valid Word Abbreviation有效的单词缩写
[抄题]: Given a non-empty string s and an abbreviation abbr, return whether the string matches with th ...
- 批量添加数据SqlBulkCopy
using System.Data.SqlClient; class Program { static void Main() { string connectionString = GetConne ...
- shell chmod中数字与字母的含义
数字与字母的组合是chmod命令赋予文件,目录访问权限的方式 访问权限:可读,可写,可执行 字母表示:r , w , x 数字表示:4 , 2 , 1 , ...
- 【原创】ListView快速滚动至新添加一行(自动滚动)
在C#开发中我们经常要开发一些日志系统,尤其是基于ListView的日志显示系统.但是当日志增多是你是否有一些困扰,就是它为什么不会自动滚动至最后一行. 以下是一小段代码,希望可以帮助你. publi ...
- redis系列:基于redis的分布式锁
一.介绍 这篇博文讲介绍如何一步步构建一个基于Redis的分布式锁.会从最原始的版本开始,然后根据问题进行调整,最后完成一个较为合理的分布式锁. 本篇文章会将分布式锁的实现分为两部分,一个是单机环境, ...
- oracle 中 创建序列sequence
drop sequence SEQ_YCXWP_CGD; create sequence SEQ_YCXWP_CGD increment start nomaxvalue;
- ASP.NET伪静态配置
一.下载URLRewriter.dll 二.在项目中添加URLRewrite的引用 三.配置webconfig 1.在<configuration>节点中添加: <configSec ...
- .net在线HTML编辑器
//在线网页编辑器, <script> var editor2 = new baidu.editor.ui.Editor({//实例化编辑器 initialContent: '', min ...
- angular 中间人模式
import { Component, OnInit, EventEmitter, Output } from '@angular/core'; @Component({ selector: 'app ...