【LeetCode】【Linked List】Merge Two Sorted Lists

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... 阅读详情

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

题译:

合并两个排序的链接列表,并将其作为新列表返回。 新列表应该通过将前两个列表的节点拼接在一起来进行。

Solution:

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
public class Solution {
    public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
        if(l1==null)
            return l2;
        if(l2==null)
            return l1;
        if(l1.val<=l2.val){
            l1.next = mergeTwoLists(l1.next,l2);
            return l1;
        }else{
            l2.next = mergeTwoLists(l1,l2.next);
            return l2;
        }
    }
}

心得:

这是一道单链表排序题,核心思想:递归排序。

小白水平理解面试经典题目LeetCode 21. Merge Two Sorted Lists【Linked List类】 Linked List 数据结构也在面试中经常出现,作为很好处理客户信息存储的结构很方便,也是重点必会项目之一,看看我们如何教懂白月光,成功邀约看电影吧。 阅读详情

相关推荐

开源CMDB与资产管理平台对比报告

本文对比分析了9款主流开源CMDB平台,从集成能力、功能完整性和多语言支持三个维度进行评测。重点介绍了VeCMDB、蓝鲸CMDB、WeCMDB和OpsAny等国产平台的特色:VeCMDB以动态模型和自动发现见长;蓝鲸CMDB适合超大规模企业级应用;WeCMDB专注金融级高并发场景;OpsAny提供一体化运维解决方案。针对不同规模企业给出选型建议:中小团队推荐Snipe-IT等轻量工具;大型企业建议蓝鲸或WeCMDB;需国产化适配的可选VeCMDB或OpsAny。文章还特别指出iTop、GLPI等适合ITIL

huang0612123的博客 1094

LeetCode 21 Merge Two Sorted Lists(合并两个已排序的链表)(Linked List)

翻译合并两个排好序的链表,并返回这个新链表。 新链表应该由这两个链表的头部拼接而成。原文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.代码/** * Defini

nomasp 1万+

cmu15445 2023fall project4 详细过程(包括Bonus Tasks) Concurrency Control

CMU15-445 23fall project 4详解

LA的博客 1612

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.        首先拿到这道题目的时候,我觉得挺简单的,严蔚敏那本数据结构里面有这样的一个例题,我

a15994269853的专栏 5435

UE5网络模块实战:属性复制、RPC与联机开发踩坑指南

多人联机开发中,网络同步是核心难题。UE5网络模块以服务器权威为基础,通过Actor复制、RPC和所有权机制实现多端状态一致。理解属性复制与OnRep回调、区分Server/Client/Multicast RPC的适用场景,是构建稳定联机架构的关键。本文结合数字孪生与多端协作场景,探讨如何将渲染与逻辑分离,并针对打包后网络失效、端口防火墙、崩溃排查等工程实践问题给出解决方案。通过蓝图接口解耦表现层,利用C++创建StaticMesh组件实现服务器逻辑与客户端表现分离,同时注意专用服务器不加载渲染依赖(如D

weixin_29010003的博客 277

Allan方差读图分析IMU误差指标

Allan方差标定IMU参数实测分析

weixin_44927558的博客 3575

Merge two sorted linked list

method 1 dummy node /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */ class Solution { pub

sumIv1210的博客 337

VFIO框架源码分析(一)- 从初始化到设备访问

本文深入剖析了Linux内核中的VFIO(Virtual Function I/O)框架,详细解读了其从初始化到设备访问的全过程。VFIO通过Container->Group->Device的层次化设计,利用IOMMU提供安全隔离,允许用户空间直接访问物理设备。文章基于Linux v5.15版本,重点分析了vfio_init模块入口、Container创建与配置、Group关联以及最终设备访问等关键环节,揭示了VFIO如何通过逐层授权机制构建安全可靠的设备直通模型,为虚拟化技术提供核心支持。

qq_36071928的博客 388

LeetCode: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-&gt;2-&gt;4, 1-&gt;3-&gt;4 Output:...

慢慢积累 1301

uniapp - 详细手机端h5网页调用手机摄像头进行二维码识别扫描,在uniapp的H5移动端微信网页平台中,在浏览器中调用手机扫码功能详细教程,识别二维码并获取扫描结果(完整示例源码,一键复制即用

uniapp,h5,手机端网页,H5手机网页,uniapp开发h5实现扫码功能,uniapp微信h5网页扫码sdk使用教程,uniapp h5网页如何实现浏览器扫码,uniapp扫码功能怎么兼容h5_uniapph5怎么做扫码功能,uniapp的手机网页如何实现扫二维码一维码,浏览器H5实现js扫码,H5调用摄像头和扫一扫功能,亲测可用,H5调用本地摄像头实现扫一扫,uni-h5项目解决扫码功能,uniapp实现微信H5扫码,解决uniapp在H5端使用不能扫码问题,uni-app的h5扫码(二维码、条形码

✅ 精选推荐文章 1万+

YOLO11涨点优化:半监督学习 | 结合Unbiased Teacher框架,利用海量无标签数据实现自训练暴力涨点

本文提出了一种基于Unbiased Teacher v2的半监督学习方法,用于解决YOLO11目标检测模型在标注数据不足时的性能瓶颈问题。通过分析纯监督YOLO11在小样本场景下的局限性,揭示了朴素自训练方法的三大陷阱:确认偏差、类别不均衡和噪声累积。文章详细解析了Unbiased Teacher框架的核心设计理念,包括教师-学生模型互监督机制、类别平衡损失和Burn-In预热策略,并重点介绍了v2版本针对无锚框检测器的优化。最后提出将Unbiased Teacher与YOLO11相结合的融合架构设计方案,

2601_95807009的博客 214

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 by splicing together the nodes of the first two lists. Example: Input: 1->2->4, 1-...

baobo4566的博客 154

使用Matlab simulink的PLC coder功能进行算法搭建与处理

使用的案例如下: Anti-Windup PID Control Demonstration with Feedforward Control 将其中的PID块生成SCL语言再导入TIA 使用的平台如下: Matlab 2019b TIA portal 15.1 应用:可以在PLC中搭载一些并不方便使用的,或者比较难使用的算法 步骤如下: 首先打开案例,案例地址我放在最下面,点击Run运行仿真,必...

苦走一口田 1万+

LeetCode 21.Merge Two Sorted Lists Easy/Linked List

文章目录1.Description2.Example3.Solution1.创建新链表一个一个添加2.一个链表向另一个链表插入 1.Description Merge two sorted linked lists and return it as a new sorted list. The new list should be made by splicing together the nodes of the first two lists. 2.Example Input: l1 = [1,2,

押切徹的博客 139

廖雪峰Python教程[完整版]

廖雪峰的python教程整理的,已经排好版了,而且还是完整的,适合小白

TMS_WebGMaps_1.9.8.0_Full_Source

TMS_WebGMaps_1.9.8.0_Full_Source 全源码 delphi cbuilder

LeetCode_Linked List_Merge Two Sorted Lists

21.Merge Two Sorted Lists1. 问题描述:合并两个有序链表,并返回一个新的有序链表。2. 解决思路:这道题很简单。可以用递归求解,也可以用非递归求解;注意:如果用非递归求解,发现新链表的头结点不确定,所以引入dummy节点。不多说之间上代码。3. java代码:/** * Definition for singly-linked list. * public class

亮子的专栏 1159

WINCC7.3破解文件

WINCC7.3硬件够破解文件,有需要的下载!WINCC7.3硬件够破解文件,有需要的下载!

LeetCode(Linked List)21. Merge Two Sorted Lists

LeetCode(Linked List)21. Merge Two Sorted Lists

weixin_45428910的博客 198

VB摄像头监控

该VB模块可对摄像头捕获的画面进行录像等,可在创建的窗口进行控制

LeetCode 21. Merge Two Sorted Lists - 链表(Linked List)系列题3

解题思路:跟合并两个有序数组是一样的。用两个指针分别指向两个链表头,比较两个指针指向的当前节点值,值小的那个肯定是两个链表剩下所有节点中最小的那个,把该节点取出放入结果中,指针指向该节点的下一个节点,然后继续比较两指针指向的当前节点值,如此反复直到其中一个指针走到链表结尾,把另一个链表剩下的所有节点一次都放入结果中,这样就完成了两个有序链表的合并。

hgq522的博客 585

Leetcode日练笔记33 [linked list专题] #21 #2 Merge Two Sorted Lists & Add Two Numbers

Leetcode Linked List专题 #21 Merged Two Linked List & #2 Add Two Numbers Python解法

Bittersweet_t的博客 300

【LeetCode】32.Linked List — 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 Outpu...

weixin_30451709的博客 174

[LeetCode] 21. Merge Two Sorted Lists_Easy tag: Linked List

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

weixin_30737433的博客 99

[Leetcode with python] 21. Merge Two Sorted Lists (# Linked list)

题目 https://leetcode.com/problems/merge-two-sorted-lists/ 解题 题目的意思是合并成一条新的有序链表。可以用递归思想,非常简单。 链表就像是一条腿的二叉树。(从单点看) # Definition for singly-linked list. # class ListNode(object): # def __init__(self,...

山上之城 320

LeetCode不定时刷题——Merge two sorted linked lists and return it as a new list

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. /** * Definition for singly-linked list. * public class ...

loserChen的博客 318

Leetcode Python-- 21 Merge Two Sorted Lists--数据结构基础---linked list

由于本人跨专业,数据结构基础较为薄弱,写这道题的时候参考了discussion区的答案,把答案读明白了,做了标注,现在这儿简单记录一下,这道题主要是要了解一下链表的基本机构以及指向是通过node1.next=node2这种赋值语句来做的,所谓排序就是在2个list当中找一个升序的指向,最后返回的是p1所以要在最后对p1先为空的情况进行判断。有空系统写一下python链表的相关知识以及常见习题类型和...

SSSevon 217

Leetcode_Linked_List --21. Merge Two Sorted Lists [easy]

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

Ling的博客 163
上一篇: 【Java】Stack相关学习
下一篇: 【Leetcode】【Stack】【String】Remove Duplicates from Sorted Array
h_hechen
博客等级 码龄11年 1粉丝 16原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值
⌂ 换网址