/**
* 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.
*/
/*
由于链表只能从前向后添加的特点,所以从前边开始比较,小的添加进去
每次递归确定一个node,需要确定两个值,一个是val,一个是next,next由下次递归确定
*/
public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
if (l1 == null)
return l2;
if (l2 == null)
return l1;
if (l1.val < l2.val)
{
ListNode cur = l1;
cur.next = mergeTwoLists(l1.next,l2);
return cur;
}
else
{
ListNode cur = l2;
cur.next = mergeTwoLists(l1,l2.next);
return cur;
} }

[leetcode]21Merge Sorted ListNode递归合并顺序链表的更多相关文章

  1. 21. Merge Two Sorted Lists (Java 合并有序链表 空间复杂度O(1))

    题目: Merge two sorted linked lists and return it as a new list. The new list should be made by splici ...

  2. LeetCode(23):合并K个排序链表

    Hard! 题目描述: 合并 k 个排序链表,返回合并后的排序链表.请分析和描述算法的复杂度. 示例: 输入: [   1->4->5,   1->3->4,   2-> ...

  3. LeetCode第21题:合并两个有序链表

    题目描述: 将两个有序链表合并为一个新的有序链表并返回.新链表是通过拼接给定的两个链表的所有节点组成的. 示例: 输入:1->2->4, 1->3->4 输出:1->1- ...

  4. 61.Merge k Sorted Lists(合并k个排序链表)

    Level:   Hard 题目描述: Merge k sorted linked lists and return it as one sorted list. Analyze and descri ...

  5. Leetcode#88. Merge Sorted Array(合并两个有序数组)

    题目描述 给定两个有序整数数组 nums1 和 nums2,将 nums2 合并到 nums1 中,使得 num1 成为一个有序数组. 说明: 初始化 nums1 和 nums2 的元素数量分别为 m ...

  6. LeetCode:Convert Sorted Array to Binary Search Tree,Convert Sorted List to Binary Search Tree

    LeetCode:Convert Sorted Array to Binary Search Tree Given an array where elements are sorted in asce ...

  7. [LeetCode] Remove Linked List Elements 移除链表元素

    Remove all elements from a linked list of integers that have value val. Example Given: 1 --> 2 -- ...

  8. LeetCode :: Convert Sorted Array (link list) to Binary Search Tree [tree]

    1.Given an array where elements are sorted in ascending order, convert it to a height balanced BST. ...

  9. [LeetCode 206] Reverse Linked List 翻转单链表

    本题要求将给定的单链表翻转,是校招面试手撕代码环节的高频题,能很好地考察对单链表这一最简单数据结构的理解:可以使用迭代和递归两种方法对一个给定的单链表进行翻转,具体实现如下: class Soluti ...

随机推荐

  1. PTA天梯赛校内模拟

    最长对称子串 || 区间dp || 马拉车 dp[i][j]表示区间[i, j]是否为回文串,若是则为1,不是则为0. 边界条件: 1. 区间长度为1,dp为1.(奇数个字符递推的起始情况) 2. 区 ...

  2. 前端静态站点在阿里云自建 K8S DevOps 集群上优雅的进行 CI/CD

    目录 网站 域名 K8S DevOps 集群 私有 Gitlab 使用 Docker 编译站点 * Dockerfile * 构建编译 Image * 测试编译 Image * 推送镜像到 Aliyu ...

  3. 从docker介绍及其简介

    一.引言 1.我本地代码运行没问题啊,但是别人机器运行不了,从而导致环境不一致的问题 2.那个兄弟又写死循环了,怎么这么卡,在多用户的操作系统下,会相互影响. 天猫双十一的情况下,用户量暴涨,从而导致 ...

  4. 【Usaco 2009 Silver】JZOJ2020年9月19日提高B组T1 音乐节拍

    [Usaco 2009 Silver]JZOJ2020年9月19日提高B组T1 音乐节拍 题目 Description FJ准备教他的奶牛弹奏一首歌曲,歌曲由N(1<=N<=50,000) ...

  5. day4(JWT介绍)

    1.JWT介绍 1.1jwt原理 最简单理解:jwt本质就是, 把用户信息通过加密后生成的一个字符串 JWT的原则是在服务器身份验证之后,将生成一个JSON对象并将其发送回用户 { "Use ...

  6. 在Python实现print标准输出sys.stdout、stderr重定向及捕获的简单办法

    专栏:Python基础教程目录 专栏:使用PyQt开发图形界面Python应用 专栏:PyQt入门学习 老猿Python博文目录 Python中的标准输出和错误输出由sys模块的stdout.stde ...

  7. 第12.3节 Python math模块导览

    math 模块提供对浮点数学的底层C库函数的访问,常用的成员包括: math.ceil(x):返回 x 的上限,即大于或者等于 x 的最小整数 math.floor(x):返回 x 的向下取整,小于或 ...

  8. 面试阿里,字节,腾讯90%被问到的springboot常见面试题,这次给你讲的明明白白!

    1.什么是Spring Boot? 多年来,随着新功能的增加,spring变得越来越复杂.只需访问https://spring.io/projects页面,我们就会看到可以在我们的应用程序中使用的所有 ...

  9. [GYCTF2020]Blacklist

    这题是用堆叠注入,同时也是借这题记录一下CTF中堆叠注入的一些骚操作 以下部分内容转载大佬的文章 show databases; 获取数据库名 show tables; 获取表名 show colum ...

  10. Spring Cloud微服务学习笔记

    Spring Cloud微服务学习笔记 SOA->Dubbo 微服务架构->Spring Cloud提供了一个一站式的微服务解决方案 第一部分 微服务架构 1 互联网应用架构发展 那些迫使 ...