Leetcode 21. Merge Two Sorted Lists

天融信防火墙NGFW4000(TG-4628)安装配置笔记 配置一台全新的天融信防火墙NGFW4000,配置完后,内网用户10.10.1.0/24和10.10.2.0/24可以通过防火墙上网,外网用户可以通过访问防火墙的外网接口地址来访问内网的WEB服务器10.10.1.200,并且内网用户也可以通过WEB服务器的外网地址进行访问。 网络说明: 防火墙外网接口地址:218.90.123.121/30 防火墙内网接口地址:19... 阅读详情

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


题目链接:https://leetcode.com/problems/merge-two-sorted-lists/description/

题目大意:合并两个有序链表为一个有序链表

解题思路:之前不太会用链表,这题是用来熟悉链表结构体的操作的。

代码:

#include <cstdio>
#include <cstring>
#include <cmath>
#include <iostream>
#include <vector>
#include <string>
#include <queue>
#include <algorithm>
#include <cmath>
#include <map>
#include <stack>
using namespace std;

struct ListNode {
    int val;
    ListNode *next;
    ListNode(int x) : val(x), next(NULL) {}
};
 
class Solution {
public:
    ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {
        ListNode* head = new ListNode(0);
        ListNode* ans = head;
        while(l1 != NULL || l2 != NULL){  
            if(l1 == NULL){
                head -> next = new ListNode(l2 -> val); 
                head = head ->next;
                l2 = l2 -> next;   
                continue;
            }else if(l2 == NULL){
                head -> next = new ListNode(l1 -> val); 
                head = head ->next;
                l1 = l1 -> next;
                continue;   
            }
            int a = l1 -> val;
            int b = l2 -> val;
            if(a > b){
                head -> next = new ListNode(b);
                head = head ->next;
                l2 = l2 -> next;

            }else{
                head -> next = new ListNode(a);
                head = head ->next;
                l1 = l1 -> next;
            }
        }
        ans = ans -> next;
        return ans;
    }
};
int main(void){
    int n,m,x;
	// vector <string> vt;
    scanf("%d%d",&n,&m);
	Solution so;
    ListNode* head1 = new ListNode(0);
    ListNode* l1  = head1;
    for(int i = 0;i < n ; i ++){
        scanf("%d",&x);
        head1 -> next = new ListNode(x);
        head1 = head1 -> next  ;
    }
    ListNode* head2 = new ListNode(0);
    ListNode* l2 = head2;
    for(int i = 0;i < m ; i ++){
        scanf("%d",&x);
        head2 -> next = new ListNode(x);
        head2 = head2 -> next;
    }
    l1 = l1 -> next;
    l2 = l2 -> next;
    ListNode* ans = so.mergeTwoLists(l1,l2);
    while(ans != NULL){
    	cout << ans -> val <<endl;
        ans = ans -> next;
    }
}





华为HCIP的学习大纲和计划 HCIP(华为认证 ICT 高级工程师)核心围绕华为 ICT 技术体系,不同方向(如路由交换、云计算、安全等)课程目录不同,以下是最热门的。 阅读详情

相关推荐

双足肌肉骨骼机器人

Clone Alpha的技术突破标志着人类进入生物-机械融合的新纪元,但其影响远超技术范畴。从伦理框架重建(如拉马克进化算法中的知识传递机制)到经济模式革新(如UBI应对机器人失业潮),需全球协作制定前瞻性政策。若放任技术无序扩张,人类社会或将面临“能力断层”与“控制权丧失”的双重危机;反之,若以人类福祉为核心导向,仿生机器人亦可成为弥合残障歧视、提升整体文明层级的钥匙。当下选择,将决定人类究竟是技术的创造者,还是被重构的客体。

百态·数智思维 | 聚焦大数据、量化与未来AI 996

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.思路类似于有序数组的归并排序. 这里需要注意的是如何设置head, 和如何交换l1, l2.code/**

_menglan_Zi 652

STM32 HAL库定时器中断实现步进电机S型加减速控制与脉冲精准计数

本文详细介绍了如何使用STM32 HAL库定时器中断实现步进电机的S型加减速控制与脉冲精准计数。通过CubeMX配置定时器、计算PSC和ARR值,并结合S型曲线算法,实现平滑启停和精准定位。文章还提供了脉冲计数方案和常见问题排查技巧,帮助开发者优化步进电机控制性能。

weixin_29189363的博客 378

【LeetCode】【Linked List】Merge Two Sorted Lists

【21】 Merge Two Sorted ListsMerge 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.题译:合并两个排序的链接列表,并将其作为新列表返回。 新列表应该通

奋斗的小晨晨 359

手把手教你用STM32F103C8T6单片机,做一个即插即用的USB游戏手柄(附完整代码)

本文详细介绍了如何使用STM32F103C8T6单片机制作即插即用的USB游戏手柄,涵盖硬件搭建、HID协议解析及完整代码实现。通过深入讲解USB HID协议的七大关键描述符,帮助开发者快速掌握嵌入式USB设备开发技巧,打造个性化游戏控制器。

weixin_30887919的博客 444

汇川AM系列PLC玩转CNC:手把手教你用File模式读取G代码文件(附避坑指南)

本文详细介绍了如何使用汇川AM系列PLC的File模式读取G代码文件,涵盖从CAD设计到PLC配置的全流程。特别针对路径格式、文件类型等常见问题提供避坑指南,帮助工程师快速实现CNC加工的自动化控制。

weixin_33582311的博客 418

java: 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 class ListNode{i

sinat_35803474的博客 602

基于Quartus II软件的FPGA综合实验——多功能数字钟

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档 文章目录前言一、设计要求二、设计原理三、设计过程1.数码管扫描模块2.计时模块3.闹钟模块4.闹钟响铃模块5.数码管显示模块6.整点报时功能7.模式选择模块(计时器主控电路)四、实验中遇到的困难及解决方案五、总结 前言 FPGA综合实验——多功能数字钟 基于Quartus II软件 一、设计要求 设计一个能进行时、分、秒计时的十二小时制或二十四小时制的数字钟,并具有定时与闹钟功能,能在设定的时间发出闹铃音,能非常方便地对小时、分钟和秒进.

qq_53022342的博客 2万+

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

qq_34609108的博客 205

Python爬虫实战:东方财富股吧与雪球舆情数据实时爬取与情绪指标挖掘

本文详细介绍如何使用Python爬虫技术实时爬取东方财富股吧和雪球平台的讨论数据,结合股价波动构建情绪分析指标。我们将使用最新的异步爬虫技术、自然语言处理方法和可视化工具,为量化交易提供数据支持。文章包含完整的代码实现和技术细节,适合有一定Python基础的读者学习。

2201_76125261的博客 1235

【教程4>第2章>第18节】 GMSK调制系统的FPGA开发与matlab对比验证

GMSK(高斯最小频移键控)是一种连续相位的频移键控(CPFSK)调制方法,它在数字通信中得到了广泛应用,特别是在移动通信系统中。GMSK通过限制频率偏差的累积来减少带外辐射,并通过使用高斯滤波器对。

FPGA/MATLAB学习教程/源码/项目合作开发 1008

leetcode 21. Merge Two Sorted Lists和2. Add Two Numbers

【代码】leetcode 21. Merge Two Sorted Lists和2. Add Two Numbers。

weixin_41554427的博客 445

OpenDataLab 大模型训练数据集下载记录

大模型 训练数据集

u013558123的博客 3504

leetcode解题报告21. Merge Two Sorted Lists

leetcode解题报告21. Merge Two Sorted Lists题目地址 难度是easy题目描述合并两条有序的链表我的思路题目比较简单,是一条数据结构方面的题目,难点是链表的操作。 此外,这个题目有一点问题,就是没有显式的说明这个排序是升序还是降序。这样更合理的方式应该通过传入的链表进行检验才行。但是这就比较麻烦了,我还是按照升序来写代码而已。我的代码/** * Definition

sysu_xiaoming的博客 337

霍尼韦尔SoftMaster使用说明

霍尼韦尔SoftMaster使用说明

IMSL CNL 7.0.0 (x86-64)Crack

IMSL CNL 7.0.0 (x86-64)Crack intel的算法库破解文件,这是C++版本的破解文件

LeetCode第21题Merge Two Sorted Lists

LeetCode第21题Merge Two Sorted Lists 下面是实现的代码 class Solution: def mergeTwoLists(self, l1, l2): """ :type l1: ListNode :type l2: ListNode :rtype: ListNode ...

weixin_30877755的博客 106

大中型超市收银系统(可直接运行)

光步前台收银系统系统功能:商品销售、商品退货、挂单、收银员权限、会员销售、单品取消、整单取消、商品查询、脱机销售、脱机数据上传,基本资料下载,开钱箱、小票打印、收款结算,支持多币种付款方式(现金、储值卡、礼券、微信等),支持多种促销。运行环境:.net 4.0登陆信息用户名:1001 密码:123456内置商品信息,商品编码可输入00001000 - 00010000,测试效果相关功能键F5 结算 F9挂单 F11商品查询 F8退货 Insert单品取消 Del整单取消 F12开钱箱F2 会员 Ctrl+Z退出系统 Home 锁屏 相关源码待公开

LeetCode力扣 21. 合并两个有序链表 Merge Two Sorted Lists 题解代码 JavaScript

问题https://leetcode-cn.com/problems/merge-two-sorted-lists/ 练习使用JavaScript解答 /** * Definition for singly-linked list. * function ListNode(val) { * this.val = val; * this.next = null; * } */ /** * @param {ListNode} l1 * @param {ListNode} .

Coding home - 漂流瓶jz 689

LeetCode 21. Merge Two Sorted Lists(合并两个有序链表) -- c语言

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

d_benhua的博客 366

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. 思路 把两个有序链表归并成一个有序链表。先用比较笨但很简洁的办法:遍历两个有序链表的节点,比较值

buptlrw的专栏 1381

【LeetCode】21. Merge Two Sorted Lists

我的个人微信公众号:Microstrong 微信公众号ID:MicrostrongAI 微信公众号介绍:Microstrong(小强)同学主要研究机器学习、深度学习、计算机视觉、智能对话系统相关内容,分享在学习过程中的读书笔记!期待您的关注,欢迎一起学习交流进步! 知乎主页:https://www.zhihu.com/people/MicrostrongAI/activities Github:https://github.com/Microstrong0305 个人博客:https://blo.

Microstrong 648

LeetCode 21. Merge Two Sorted Lists 题解

本题是链表合并的经典问题。迭代方法:虚拟头节点的使用递归方法:递归的终止条件处理剩余节点通过本题可以深入理解链表的基本操作。

cannonjinx的博客 4172

【leetcode c++】21 Merge Two Sorted Lists

Merge two sorted linked lists and return itas a new list. The new list should be made by splicing together the nodes ofthe first two lists. 融合两个有序的链表,返回这个新链表。   其实呢,这也是数据结构里面的链表的题(类似多项式的加法),不过稍微比当年

lif的博客 1111

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. 分析如下: 比较简单,试了一下,10分钟在leetcode网页上敲完且检查了一下bug,提交发

BUILD 1349

21. Merge Two Sorted Lists【力扣】

题意理解 合并两个有序单链表,新链表也是有序。 问题分析 三个循环,第一个循环如果两个链表都不空,比较当前链表值的大小,如果第一个链表值小,当前第一个链表值装入新链表,后移指针;如果第二个链表值小,当前第二个链表值装入新链表,后移指针; 第二个循环如果第一个链表不空,将后续链表接入新链表 第三个循环如果第二个链表不空,将后续链表接入新链表 技巧点:这里没有头结点,在接入新链表时有点麻烦...

xiexie1357的专栏 155

LeetCode (力扣) 21. Merge Two Sorted Lists (C) - Easy

同步发于JuzerTech网站,里面有我软、硬件学习的纪录与科技产品开箱,欢迎进去观看。 此题测验的是链表 ( Linked List ) ,把两个 Linked List 依照大小排序,串接在一起,并回传串接完的 Linked List。 ...

JuzerTech的博客 186

LeetCode 21. Merge Two Sorted Lists

题目来源:https://leetcode.com/problems/merge-two-sorted-lists/submissions/ 问题描述 21.Merge Two Sorted Lists Easy Merge two sorted linked lists and return it as a new list. The new list should be made b...

da_kao_la的博客 133
上一篇: Leetcode 26. Remove Duplicates from Sorted Array
下一篇: Leetcode 3. Longest Substring Without Repeating Characters
雨狮子
博客等级 码龄12年 24粉丝 185原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值
⌂ 换网址