LeetCode #21 - Merge Two Sorted Lists

别再手动续期SSL证书了!用acme.sh配合阿里云DNS API,实现全自动HTTPS证书管理 本文详细介绍了如何使用acme.sh配合阿里云DNS API实现SSL/TLS证书的全自动管理,彻底解决手动续期带来的风险与不便。通过自动化部署流程,包括环境准备、API集成、多域名证书申请等关键步骤,帮助运维人员构建高效、可靠的HTTPS证书管理体系,显著提升安全性和运维效率。 阅读详情

题目描述:

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.


此题可以直接模拟归并排序的步骤,但是要小心指针为空的情况。

class Solution {
public:
    ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {
        ListNode* result=new ListNode(0);
        ListNode* head=result;
        if(l1==NULL&&l2==NULL) return NULL;
        while(l1!=NULL||l2!=NULL)
        {            
            ListNode* temp=new ListNode(0);
            if(l1==NULL&&l2!=NULL) 
            {
                temp->val=l2->val;
                result->next=temp;
                result=result->next;
                l2=l2->next;
            }
            else if(l1!=NULL&&l2==NULL) 
            {
                temp->val=l1->val;
                result->next=temp;
                result=result->next;
                l1=l1->next;
            }
            else if(l1->val>l2->val)
            {
                temp->val=l2->val;
                result->next=temp;
                result=result->next;
                l2=l2->next;
            }
            else 
            {
                temp->val=l1->val;
                result->next=temp;
                result=result->next;
                l1=l1->next;
            }
        }
        return head->next;
    }
};


另外,这道题还能用递归的方法求解,代码显得更加简洁,这种构建链表的递归方法在之前的LeetCode #2 - add two numbers也用到过。
class Solution {
public:
    ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {
        ListNode* head=new ListNode(0);
        if(l1==NULL&&l2==NULL) return NULL;
        if(l1!=NULL&&l2==NULL) head=l1;
        else if(l1==NULL&&l2!=NULL) head=l2;
        else if(l1!=NULL&&l2!=NULL)
        {
            if(l1->val>l2->val)
            {
                head->val=l2->val;
                head->next=mergeTwoLists(l1,l2->next);
            }
            else 
            {
                head->val=l1->val;
                head->next=mergeTwoLists(l1->next,l2);
            }
        }
        return head;
    }
};



【LDPC-7】LLR量化位宽对LDPC译码性能的影响分析——以最小和译码算法为例进行matlab仿真 最小和译码算法的步骤如下:最后译码结果如果满足或者完成所有迭代次数,则译码算法停止迭代,否则继续进行译码迭代操作。 阅读详情

相关推荐

SIwave:在 SIwave 中释放计算频率扫描的强大功能

AEDT 的电路工具允许注入正确的电压和电流。要使用电路工具,SIwave 必须首先使用 PI 求解器求解结构的 S 参数。

jkh920184196的博客 1474

LeetCode Notes_#21 Merge Two Sorted Lists

LeetCode Notes_#21 Merge Two Sorted ListsLeetCode Contents 题目思路和解答思路解答 题目 Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the ...

baji9701的博客 166

HDMI与DisplayPort混搭方案:如何用Xilinx Video PHY Controller实现多协议视频接口转换

本文详细解析了如何利用Xilinx Video PHY Controller IP核实现HDMI与DisplayPort协议转换,涵盖混合视频接口架构设计、GT收发器动态重配置策略及跨协议时钟域处理方案。通过实战案例和配置技巧,帮助开发者构建高性能多协议视频转换系统,适用于医疗影像、工业控制等场景。

weixin_30776273的博客 446

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. 思路: 设置两个头指针,一个指针不动,用来返回最后头结点,一个指针用来组织节点顺序,遍

胸口好想碎大石 914

旅行青蛙破解汉化教程——蹭个热度

汉化破解版资源(无尽三叶草与点券): https://github.com/wisdombyzf/lxqw/blob/master/lxqw_zf.apk 截图: 破解方法: 工具: 1,APKIDE https://www.pd521.com/thread-818-1-2.html 2,dnspy https://down.52pojie.

github_38838414的博客 3783

电磁场数值分析作业(西电)共5次【2021】

文章目录作业1作业2作业3作业4作业5 作业1 一个二维正方形(边长a=10mm)的静电场区域,电位边界条件如图所示(单位:V),求区域内的电位分布。要求用超松弛迭代法求解差分方程组进行计算。 clc clear close all hx=11; hy=11; v1=zeros(hy,hx); v1(hy,:)=ones(1,hx)*100; v1(1,:)=ones(1,hx)*50; for i=1:hy v1(i,1)=0; v1(i,hx)=100; end w=2/(1+

薛定谔的壳的博客 5071

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

zyp7355的博客 102

Proteus下载安装快速入门:30分钟掌握全过程

手把手教你完成proteus下载安装,从环境配置到首次运行一气呵成,适合电子设计新手快速掌握仿真工具的核心操作步骤。

weixin_31974443的博客 891

leetcode #21 Merge Two Sorted Lists

这题是简单的merge 不过思路一定要清晰。public class Solution { public ListNode mergeTwoLists(ListNode l1, ListNode l2) { if(l1==null) return l2; if(l2==null) return l1;

u010737845的博客 310

一文读懂YOLO V5 与 YOLO V4

一文读懂YOLO V5 与 YOLO V4 YOLO之父Joseph Redmon在今年年初宣布退出计算机视觉的研究的时候,很多人都以为目标检测神器YOLO系列就此终结。然而在4月23日,继任者YOLO V4却悄无声息地来了。Alexey Bochkovskiy发表了一篇名为YOLOV4: Optimal Speed and Accuracy of Object Detection的文章。YOLO V4是YOLO系列一个重大的更新,其在COCO数据集上的平均精度(AP)和帧率精度(FPS)分别提高了10%

williamhyin的博客 8788

ZYNQ 7020 之 FPGA知识点重塑笔记一——串口通信

通信方式一般分为串行通信和并行通信。并行通信是指多比特数据同时通过并行线进行传送。这种传输方式通信线多、成本高,故不宜进行远距离通信,通常传输距离小于 30 米。串行通信是指数据在一条数据线上,一比特接一比特地按顺序传送的方式。这种运输方式通常节省传输线,大大降低使用成本,但数据传送速度慢。综上可知,串行通信主要应用于长距离、低速率的通信场合。本次实验我们主要讲解下串行通信。串行通信一般有2种通信方式:同步串行通信和异步串行通信。同步串行通信。

m0_51430584的博客 3092

【一天一道LeetCode】#21. Merge Two Sorted Lists

一天一道LeetCode系列(一)题目 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. (二)解题这题是剑指offer上的老题了,剑指上面用的是递归,我写了个非递

ZeeCoder 768

华为的C语言编程规范.pdf

华为技术有限公司内部技术规范。本规范制定了编写C语言程序的基本原则、规则和建议。从代码的清晰、简洁、可测试、安全、程序效率、可移植各个方面对C语言编程作出了具体指导。

LeetCode #21 Merge Two Sorted Lists

LeetCode #21 Merge Two Sorted Lists Question 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 Appr...

weixin_30361753的博客 75

最新的sdio 4.0 spec 协议标准 带书签 高清完整PDF版

Part-E1-SDIO-Specification-Ver4.00-Final-120220.pdf:最新的sdio 4.0 spec 协议标准 带书签 高清完整PDF版,包括SDIO 接口的外设,SDIO 接口官方协议等。

C# WinForm 基于DirectShow.dll编写的程序,可边预览边保存视频,音频和图片

基于微软的DirectShow.dll的二次开发1.自动识别连接的摄像头跟麦克风且可以对其进行选择(默认选中第一个)2.可录制wmv和avi两种格式的视频并且都带有音频,avi可以录制的分辨率和帧数进行选择,wmv默认进行了淸晰度优化如不喜欢可去除代码中有相关提示3.可在预览点开之后进行拍照,也可边录制视频边拍照4.添加了视频和音频的压缩功能(酌情使用)这次二次开发发现C#对DirectShow的相关信息太少,这次做完也不敢藏私共享出来,还望大家指正

LeetCode_#21_Merge_Two_Sorted_Lists

就这破玩意儿我写了好几遍,可见基本功太差劲儿啊,药丸! class Solution { public: ListNode * mergeTwoLists(ListNode* l1, ListNode* l2) { if (l1 == nullptr&&l2 == nullptr) return l1; else if (l1 == nullptr&&...

真的想嫁给朱一龙诶 165

Leetcode学习笔记:#21. Merge Two Sorted Lists

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

ccystewart的博客 117

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

DAY9: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. Subscribe to see which companies asked this questio

zcy19941015的博客 254

LeetCode #21 Merge Two Sorted Lists C# Solution

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. 一道简单的链表题,然而我用了太久list<>已经忘记链表怎么做了= = 总而言之非常感谢http://www.cnb

草履虫早晚会长大 520

leetCode #21 Merge Two Sorted Lists

题目:将2个有序链表合并成一个有序链表 分析:思路很简单,用递归就更简单了 答案: class Solution { public: ListNode *mergeTwoLists(ListNode *l1, ListNode *l2) { if (l1 == NULL) { return l2; } if (l2 == NULL) { r

MarStarck的专栏 419

[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. 本题就是简单的链表合成。时间:14ms 我的代码: /** * Definition for singl...

weixin_30763397的博客 54

Leetcode #21 Merge Two Sorted Lists 合并有序链表 解题报告

0 题外话嗯,从这次开始我就把题目换一下,改成解题报告了,因为我小节小结总是不分,等有时间也去改下之前的解题报告咯~~我算了下自己的速度,作为一个本学期的额外爱好,到现在21天刷了100题,速度也还算好,毕竟像今天这样基本一题没法刷的时间占到一周的一半多的时间。1 解题思想题目是给两个单链表,里面的数值都是有序的,现在要求他们合并成一个有序链表。这道题的做法就是,从两个单链表的头结点开始分别设立两个

MebiuW的专栏 2168

Merge Two Sorted Lists_Leetcode_#21

1.题目 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.2.解法/** * Definition for singly-linked list. * publi

LittleBob180的专栏 263

[leetcode]#21. Merge Two Sorted Lists

按顺序拼接两个已排序的链表 class Solution(object): def mergeTwoLists(self, l1, l2): ret = ListNode(0) if l1 == None: return l2 if l2 == None: return l1 if

小草莓的博客 182

LeetCode[链表] - #21 Merge Two Sorted Lists

原题链接:#21 Merge Two Sorted Lists   要求: 合并两个已排序的单向链表,将合并后的结果作为一个链表返回。 ListNode定义: public class ListNode { int val; ListNode next; ListNode(int x) { val = x; } }   难度:简单   分析: ...

Stay hungry, stay foolish. 187
上一篇: LeetCode #19 - Remove Nth Node From End of List
下一篇: LeetCode #22 - Generate Parentheses
LawFile
博客等级 码龄10年 5粉丝 627原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值