当前位置: 首页 > news >正文

闯关leetcode——21. Merge Two Sorted Lists

大纲

  • 题目
    • 地址
    • 内容
  • 解题
    • 代码地址

题目

地址

https://leetcode.com/problems/merge-two-sorted-lists/description/

内容

You are given the heads of two sorted linked lists list1 and list2.

Merge the two lists into one sorted list. The list should be made by splicing together the nodes of the first two lists.

Return the head of the merged linked list.

Example 1:
在这里插入图片描述

Input: list1 = [1,2,4], list2 = [1,3,4]
Output: [1,1,2,3,4,4]

Example 2:

Input: list1 = [], list2 = []
Output: []

Example 3:

Input: list1 = [], list2 = [0]
Output: [0]

Constraints:

  • The number of nodes in both lists is in the range [0, 50].
  • -100 <= Node.val <= 100
  • Both list1 and list2 are sorted in non-decreasing order.

解题

这题就是要将两个从小到大已排序好的链表合并成一个仍然从小到大排序的链表。
这题没什么解题技巧,就是链表操作练习。
唯一的边界就是:某条链表遍历完后,要将另外一条链表的后续元素连接上。

class Solution {
public:ListNode* mergeTwoLists(ListNode* list1, ListNode* list2) {ListNode dummyHead;ListNode* current = &dummyHead;while (list1 != nullptr && list2 != nullptr) {if (list1->val < list2->val) {current->next = list1;list1 = list1->next;} else {current->next = list2;list2 = list2->next;}current = current->next;}if (list1 == nullptr) {current->next = list2;} else if (list2 == nullptr) {current->next = list1;} return dummyHead.next;}
};

在这里插入图片描述

代码地址

https://github.com/f304646673/leetcode/tree/main/21-Merge-Two-Sorted-Lists

http://www.lryc.cn/news/437292.html

相关文章:

  • Notepad++中提升编码效率的关键快捷键
  • ai智能语电销机器人有哪些功能?
  • ctfshow-PHP反序列化
  • BEV学习---LSS-2
  • PhpStudy下载安装使用学习
  • 在Excel中通过Python运行公式和函数实现数据计算
  • 基于SpringBoot+Vue的美妆购物系统
  • uniapp uni-table合并单元格
  • MySQL 创建数据库和表全攻略
  • 大数据-126 - Flink State 03篇 状态原理和原理剖析:状态存储 Part1
  • RFID射频模块(MFRC522 STM32)
  • 【JavaSE】--方法的使用
  • wireshark打开时空白|没有接口,卸载重装可以解决
  • 单值二叉树--(C语言)
  • Linux云计算 |【第三阶段】PROJECT1-DAY2
  • Claude Prompt 汉语新解
  • 【运维监控】influxdb 2.0+grafana 监控java 虚拟机以及方法耗时情况(2)
  • 怎么看待伦敦银交易的风险与收益?
  • 如何通俗易懂的解释TON的智能合约
  • 针对Docker容器的可视化管理工具—DockerUI
  • 五大注入攻击网络安全类型介绍
  • linux-L9.linux中对文件 按照时间排序 显示100 个
  • springboot从分层到解耦
  • 网络视频流解码显示后花屏问题的分析
  • MySQL 大量 IN 的查询优化
  • python运维
  • gen_server补充基础学习
  • Python 入门教程(3)基础知识 | 3.1、基础语法
  • git 合并分支并解决冲突
  • 《程序猿之设计模式实战 · 装饰者模式》