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

合并有序链表

合并有序链表

  • 图解
    • 代码如下

图解

虽然很复杂,但能够很好的理解怎么使用链表,以及对链表的指针类理解
在这里插入图片描述

代码如下

Node* merge_list_two_pointer(List& list1, List& list2)
{Node* new_head1 = list1.head;Node* new_head2 = list2.head;Node* sentinel1 = list1.head;Node* sentinel2 = list2.head;Node* temp_head1 = NULL;Node* temp_head2 = NULL;int flage = 1;//因为下面是<=,所以以list2优先为空if (new_head1->data >= new_head2->data){flage = 2;}while (new_head1 != NULL && new_head2 != NULL){while (list1.head != NULL && list1.head->data < list2.head->data){temp_head1 = list1.head;list1.head = list1.head->next;}//正常两个有序列表,上面为空,//456,123456789if (list1.head == NULL && flage == 2){temp_head1->next = list2.head;return sentinel2;}//特殊情况列表,全大//123,456if (list1.head == NULL){temp_head1->next = new_head2;return sentinel1;}if (temp_head1 != NULL){temp_head1->next = new_head2;}while (list2.head != NULL && list2.head->data <= list1.head->data){temp_head2 = list2.head;list2.head = list2.head->next;}//正常两个有序列表,下面为空//123456789,456if (list2.head == NULL && flage == 1){temp_head2->next = list2.head;return sentinel1;}//特殊情况列表也就是,全小//456,123if (list2.head == NULL){//防止89,89这种类型链表跑空temp_head2->next = list1.head;return sentinel2;}//这里不需要判断这个为空。如果为空,则说明已经到达链表尾部temp_head2->next = list1.head;new_head1 = list1.head;new_head2 = list2.head;}
}
http://www.lryc.cn/news/386225.html

相关文章:

  • 【SpringBoot Web框架实战教程】05 Spring Boot 使用 JdbcTemplate 操作数据库
  • Spark基于DPU的Native引擎算子卸载方案
  • Mini2440 start.s 修改支持串口输出,方便调试 (四)
  • 【教程】几种不同的RBF神经网络
  • 【Liunx-后端开发软件安装】Liunx安装FDFS并整合nginx
  • 【Java笔记】Flyway数据库管理工具的基本原理
  • 国际数字影像产业园创业培训,全面提升创业能力!
  • pyqt5 制作视频剪辑软件,切割视频
  • VUE----通过nvm管理node版本
  • R语言进行字符的替换和删减gsub,substr函数
  • 2024年6月27日,欧盟REACH法规新增第31批1项SVHC高关注物质
  • 高通410-linux棒子设置网络驱动
  • PostgreSQL的系统视图pg_stat_archiver
  • 【D3.js in Action 3 精译】第一部分 D3.js 基础知识
  • 面试经验分享 | 渗透测试工程师(实习岗)
  • STM32 IWDG(独立看门狗)
  • ios swift5 获取wifi列表
  • 回溯法c++学习 解决八皇后问题
  • 5. Spring IoCDI ★ ✔
  • 数据库自动备份到gitee上,实现数据自动化备份
  • 探索 Spring Cloud Gateway:构建微服务架构的关键一环
  • P1114 “非常男女”计划最优解
  • C++ | Leetcode C++题解之第187题重复的DNA序列
  • 构建、标记和发布镜像
  • [Go Web] Kratos 使用的简单总结
  • 首个实时 AI 视频生成技术发布;科大讯飞发布星火大模型 4.0 丨 RTE 开发者日报
  • 什么是容器镜像
  • ElasticSearch-Windows系统ElasticSearch(ES)的下载及安装
  • 【应用开发二】GPIO操控(输出、输入、中断)
  • 单点登录方法