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

L6.【LeetCode笔记】合并两个有序链表

1.题目

https://leetcode.cn/problems/merge-two-sorted-lists/

将两个升序链表合并为一个新的 升序 链表并返回。新链表是通过拼接给定的两个链表的所有节点组成的。 

示例 1:

f30bf84707351f6d3a2cead840a19fc7.jpeg

输入:l1 = [1,2,4], l2 = [1,3,4]
输出:[1,1,2,3,4,4]

示例 2:

输入:l1 = [], l2 = []
输出:[]

示例 3:

输入:l1 = [], l2 = [0]
输出:[0]

提示:

  • 两个链表的节点数目范围是 [0, 50]
  • -100 <= Node.val <= 100
  • l1l2 均按 非递减顺序 排列
  • 代码模版
  • /*** Definition for singly-linked list.* struct ListNode {*     int val;*     struct ListNode *next;* };*/
    struct ListNode* mergeTwoLists(struct ListNode* list1, struct ListNode* list2) 
    {
    }

2.自解

一个容易想到的解法:取小的尾插(开一个新的链表)

对于链表list1和list2,可以另外开一个新的链表,再将list1和list2的val复制进新链表的节点,最后返回新链表的头结点的地址即可

不加思索写出以下代码:

/*** Definition for singly-linked list.* struct ListNode {*     int val;*     struct ListNode *next;* };*/
struct ListNode* mergeTwoLists(struct ListNode* list1, struct ListNode* list2) 
{struct ListNode* cur1=list1;struct ListNode* cur2=list2;if (list1==NULL)return list2;if (list2==NULL)return list1;struct newListNode{int new_val;struct ListNode* new_next;};struct newListNode* new_next=NULL;struct newListNode* newhead=NULL;struct newListNode* m_m_new=(struct newListNode*)malloc(sizeof(struct newListNode));newhead=m_m_new;newhead->new_next=NULL;struct newListNode* new_cur=newhead;while(cur1!=NULL && cur2!=NULL){if (cur1==NULL){new_cur->new_val=cur2->val;cur2=cur2->next;//分配新结点的空间struct newListNode* m_new=(struct newListNode*)malloc(sizeof(struct newListNode));new_cur->new_next=m_new;new_cur=m_new;continue;}if (cur2==NULL){new_cur->new_val=cur1->val;cur1=cur1->next;struct newListNode* m_new=(struct newListNode*)malloc(sizeof(struct newListNode));new_cur->new_next=m_new;new_cur=m_new;continue;}if (cur1->val<=cur2->val){new_cur->new_val=cur1->val;cur1=cur1->next;struct newListNode* m_new=(struct newListNode*)malloc(sizeof(struct newListNode));new_cur->new_next=m_new;new_cur=m_new;}else{new_cur->new_val=cur2->val;cur2=cur2->next;//分配新结点的空间struct newListNode* m_new=(struct newListNode*)malloc(sizeof(struct newListNode));new_cur->new_next=m_new;new_cur=m_new;}}new_cur->new_next=NULL;new_cur=NULL;return newhead;
}

运行时出现问题

75c71bd74f524c6faa7bc7b8733a78ea.png

发现while循环的条件写错了!!

应该改成

while(!(cur1==NULL && cur2==NULL))

完整代码

struct ListNode* mergeTwoLists(struct ListNode* list1, struct ListNode* list2) 
{struct ListNode* cur1=list1;struct ListNode* cur2=list2;if (list1==NULL)return list2;if (list2==NULL)return list1;struct newListNode{int new_val;struct ListNode* new_next;};struct newListNode* new_next=NULL;struct newListNode* newhead=NULL;struct newListNode* m_m_new=(struct newListNode*)malloc(sizeof(struct newListNode));newhead=m_m_new;newhead->new_next=NULL;struct newListNode* new_cur=newhead;struct newListNode* before_new_cur=NULL;while(!(cur1==NULL && cur2==NULL)){if (cur1==NULL){new_cur->new_val=cur2->val;cur2=cur2->next;struct newListNode* m_new=(struct newListNode*)malloc(sizeof(struct newListNode));new_cur->new_next=m_new;before_new_cur=new_cur;new_cur=m_new;new_cur->new_next=NULL;continue;}if (cur2==NULL){new_cur->new_val=cur1->val;cur1=cur1->next;struct newListNode* m_new=(struct newListNode*)malloc(sizeof(struct newListNode));new_cur->new_next=m_new;before_new_cur=new_cur;new_cur=m_new;continue;}if (cur1->val<=cur2->val){new_cur->new_val=cur1->val;cur1=cur1->next;struct newListNode* m_new=(struct newListNode*)malloc(sizeof(struct newListNode));new_cur->new_next=m_new;new_cur=m_new;}else{new_cur->new_val=cur2->val;cur2=cur2->next;           struct newListNode* m_new=(struct newListNode*)malloc(sizeof(struct newListNode));new_cur->new_next=m_new;new_cur=m_new;}}before_new_cur->new_next=NULL;return newhead;
}

before_new_cur是当cur1===NULL或cur2==NULL,备份new_cur的前一个节点的地址

提交结果

6bbfcd94198846fba7ea978f264e0a9b.png

3.其他解法

方法1:取小的尾插(不开新链表)

struct ListNode* mergeTwoLists(struct ListNode* list1, struct ListNode* list2) 
{struct ListNode* cur1=list1;struct ListNode* cur2=list2;struct ListNode* head=NULL;struct ListNode* tail=NULL;if (list1==NULL)return list2;if (list2==NULL)return list1;while (cur1 && cur2){if (cur1->val<cur2->val){if (head==NULL){head=tail=cur1;}else{tail->next=cur1;tail=tail->next;}cur1=cur1->next;}else{if (head==NULL){head=tail=cur2;}else{tail->next=cur2;tail=tail->next;}cur2=cur2->next;           }}if(cur1)tail->next=cur1;if(cur2)tail->next=cur2;return head;
}

分析

尾插要有尾指针tail(这样不用频繁找尾),同时要有指向头节点的指针head用于返回

cur1->val<cur2->val和cur1->val>=cur2->val操作方式是类似的

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

相关文章:

  • 讲解Golang选择语句
  • 练习LabVIEW第四十一题
  • 应对AI与机器学习的安全与授权管理新挑战,CodeMeter不断创新引领保护方案
  • 【2024最新版Kotlin教程】Kotlin第一行代码系列第五课-类继承,抽象类,接口
  • 虚拟现实和增强现实技术,如何打造沉浸式体验?
  • ChatGPT任务设计和微调策略的优化
  • 通过 SSH 连接远程 Ubuntu 服务器
  • Perl 环境安装
  • 【NOIP提高组】引水入城
  • openvino python推理demo
  • JavaWeb项目-----博客系统
  • GY-56 (VL53L0X) 激光测距
  • 当今陪玩系统小程序趋势,陪玩系统源码搭建后的适用于哪些平台
  • qt QListWidget详解
  • java ssm 校园快递物流平台 校园快递管理系统 物流管理 源码 jsp
  • 西安电子科技大学考研网报审核通过了,然后呢?
  • pandas习题 051:将字符串数据读取到 DataFrame
  • 改进探路者算法复现
  • PostgreSQL 学习笔记:PostgreSQL 主从复制
  • 【系统架构设计师(第2版)】十一、未来信息综合技术
  • Pytorch学习--神经网络--优化器
  • w~自动驾驶合集11
  • 大数据新视界 -- 大数据大厂之 Impala 性能优化:解锁大数据分析的速度密码(上)(1/30)
  • GESP4级考试语法知识(算法概论(三))
  • x-cmd pkg | gum - 轻松构建美观实用的终端界面,解锁命令行新玩法
  • WMS系统打通仓储全链条数据势在必行,该如何做呢
  • 基于Python的校园爱心帮扶管理系统
  • 如何基于pdf2image实现pdf批量转换为图片
  • Tomcat(1) 什么是Tomcat?
  • 商务礼仪与职场沟通