码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • 【数据结构入门_链表】 Leetcode 21. 合并两个有序链表


    原题连接: Leetcode 21. Merge Two Sorted Lists

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

    Merge the two lists in a 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]
    
    • 1
    • 2

    Example 2:

    Input: list1 = [], list2 = []
    Output: []
    
    • 1
    • 2

    Example 3:

    Input: list1 = [], list2 = [0]
    Output: [0]
    
    • 1
    • 2

    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.

    方法一:迭代

    思路:

    先建立一个虚拟头结点prehead,和一个指向虚拟头结点的指针prev。返回的时候返回prehead->head就可以,能减少很多麻烦的边界问题。
    两个指针遍历两个链表。每次选择关键字小的结点接到prev上即可。
    注意最后需要扫尾,把循环结束没遍历完的链表的余下部分直接接上去。
    这个扫尾的思想用的太多了,类似于归并排序

    c++代码:

    /**
     * Definition for singly-linked list.
     * struct ListNode {
     *     int val;
     *     ListNode *next;
     *     ListNode() : val(0), next(nullptr) {}
     *     ListNode(int x) : val(x), next(nullptr) {}
     *     ListNode(int x, ListNode *next) : val(x), next(next) {}
     * };
     */
    class Solution {
    public:
        ListNode* mergeTwoLists(ListNode* list1, ListNode* list2) {
            // 创建虚拟头结点prehead, 值为-1    prev为指向虚拟头结点的指针
            ListNode* preHead = new ListNode(-1);
            ListNode* prev = preHead;
    
            // 双指针遍历两个链表
            while(list1 != nullptr && list2 != nullptr){
                // 找到小的结点
                if(list1->val < list2->val){
                    prev->next = list1;
                    list1 = list1->next;
                } else {
                    prev->next = list2;
                    list2 = list2->next;
                }
                prev = prev->next;
            }
            
            // 扫尾
             prev->next = (list1 == nullptr ? list2 : list1);
    
            return preHead->next;
        }
    };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36

    复杂度分析:

    • 时间复杂度:O(m+n),需要遍历两个链表的所有元素
    • 空间复杂度:O(1),常数个临时变量
  • 相关阅读:
    探索人工智能的深度神经网络:理解、应用与未来
    嵌入式Linux驱动开发基础知识(一)——hello驱动程序开发
    MyBatis Plus实现动态字段排序
    Nginx核心要领十五:离线安装Nginx
    java基础巩固12
    项目性能优化—性能优化的指标、目标
    C++ //练习 10.37 给定一个包含10个元素的vector,将位置3到7之间的元素按逆序拷贝到一个list中。
    密码学引论 | DES
    安卓恶意应用识别(三)(批量反编译与属性值提取)
    从路由器真机提取固件包(二)
  • 原文地址:https://blog.csdn.net/cwtnice/article/details/125536529
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号