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

递归算法:合并与反转链表的艺术

合并两个有序链表

合并两个有序链表是将两个升序排列的链表合并成一个新的升序链表。使用递归方法时,核心思路是:比较两个链表的头节点值,选择较小的节点作为新链表的头,然后递归地合并剩余部分。如果其中一个链表为空,直接返回另一个链表作为结果。

递归过程:

  • 如果链表1为空,返回链表2。
  • 如果链表2为空,返回链表1。
  • 否则,比较链表1和链表2的头节点值:
    • 如果链表1的头节点值较小,则将链表1的头节点作为新头,递归合并链表1的剩余部分和链表2。
    • 否则,将链表2的头节点作为新头,递归合并链表1和链表2的剩余部分。

C++代码实现:

struct ListNode { int val; ListNode *next; ListNode(int x) : val(x), next(nullptr) {} }; ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) { if (l1 == nullptr) return l2; // 链表1为空,返回链表2 if (l2 == nullptr) return l1; // 链表2为空,返回链表1 if (l1->val < l2->val) { l1->next = mergeTwoLists(l1->next, l2); // 递归合并剩余部分 return l1; } else { l2->next = mergeTwoLists(l1, l2->next); // 递归合并剩余部分 return l2; } }

https://gitee.com/Guhuitao/resjtrhtr/issues/IJAKP0
https://gitee.com/Guhuitao/resjtrhtr/issues/IJAKPG
https://gitee.com/Guhuitao/resjtrhtr/issues/IJAKPY
https://gitee.com/Guhuitao/resjtrhtr/issues/IJAKQC
https://gitee.com/Guhuitao/resjtrhtr/issues/IJAKQO
https://gitee.com/Guhuitao/resjtrhtr/issues/IJAKRP
https://gitee.com/Guhuitao/resjtrhtr/issues/IJAKS3
https://gitee.com/Guhuitao/resjtrhtr/issues/IJAKSM
https://gitee.com/Guhuitao/resjtrhtr/issues/IJAKTG
https://gitee.com/Guhuitao/resjtrhtr/issues/IJAKTR
https://gitee.com/Guhuitao/resjtrhtr/issues/IJAKUA
https://gitee.com/Guhuitao/resjtrhtr/issues/IJAKUQ
https://gitee.com/Guhuitao/resjtrhtr/issues/IJAKV7
https://gitee.com/Guhuitao/resjtrhtr/issues/IJAKW1
https://gitee.com/Guhuitao/resjtrhtr/issues/IJAKWH
https://gitee.com/Guhuitao/resjtrhtr/issues/IJAKWX
https://gitee.com/Guhuitao/resjtrhtr/issues/IJAKXJ
https://gitee.com/Guhuitao/resjtrhtr/issues/IJAKY2
https://gitee.com/Guhuitao/resjtrhtr/issues/IJAKYJ
https://gitee.com/Guhuitao/resjtrhtr/issues/IJAKZ1
https://gitee.com/Guhuitao/resjtrhtr/issues/IJAKZP
https://gitee.com/Guhuitao/resjtrhtr/issues/IJAL05
https://gitee.com/Guhuitao/resjtrhtr/issues/IJAL0J
https://gitee.com/Guhuitao/resjtrhtr/issues/IJAL0V
https://gitee.com/Guhuitao/resjtrhtr/issues/IJAL17
https://gitee.com/Guhuitao/resjtrhtr/issues/IJAL1H
https://gitee.com/Guhuitao/resjtrhtr/issues/IJAL1X
https://gitee.com/Guhuitao/resjtrhtr/issues/IJAL2A
https://gitee.com/Guhuitao/resjtrhtr/issues/IJAL2I
https://gitee.com/Guhuitao/resjtrhtr/issues/IJAL2R

反转链表

反转链表是将链表节点的顺序完全倒置。使用递归方法时,核心思路是:递归到链表的末尾(最后一个节点),然后从后向前调整指针方向。具体步骤是:递归调用直到当前节点的下一个节点为空(即尾节点),此时该节点成为新链表的头节点;然后逐层返回,修改指针指向。

递归过程:

  • 递归终止条件:如果当前节点为空或下一个节点为空(即尾节点),返回当前节点作为新头。
  • 递归调用:对当前节点的下一个节点进行递归,得到反转后的新链表。
  • 调整指针:将当前节点的下一个节点的指针指向当前节点,实现反转。
  • 清除当前节点的原始指针,避免循环引用。

C++代码实现:

ListNode* reverseList(ListNode* head) { if (head == nullptr || head->next == nullptr) { return head; // 终止条件:空节点或尾节点,返回头节点 } ListNode* newHead = reverseList(head->next); // 递归到末尾 head->next->next = head; // 调整指针,反转方向 head->next = nullptr; // 清除原始指针 return newHead; // 返回新头节点 }

https://gitee.com/Guhuitao/resjtrhtr/issues/IJAKP0
https://gitee.com/Guhuitao/resjtrhtr/issues/IJAKPG
https://gitee.com/Guhuitao/resjtrhtr/issues/IJAKPY
https://gitee.com/Guhuitao/resjtrhtr/issues/IJAKQC
https://gitee.com/Guhuitao/resjtrhtr/issues/IJAKQO
https://gitee.com/Guhuitao/resjtrhtr/issues/IJAKRP
https://gitee.com/Guhuitao/resjtrhtr/issues/IJAKS3
https://gitee.com/Guhuitao/resjtrhtr/issues/IJAKSM
https://gitee.com/Guhuitao/resjtrhtr/issues/IJAKTG
https://gitee.com/Guhuitao/resjtrhtr/issues/IJAKTR
https://gitee.com/Guhuitao/resjtrhtr/issues/IJAKUA
https://gitee.com/Guhuitao/resjtrhtr/issues/IJAKUQ
https://gitee.com/Guhuitao/resjtrhtr/issues/IJAKV7
https://gitee.com/Guhuitao/resjtrhtr/issues/IJAKW1
https://gitee.com/Guhuitao/resjtrhtr/issues/IJAKWH
https://gitee.com/Guhuitao/resjtrhtr/issues/IJAKWX
https://gitee.com/Guhuitao/resjtrhtr/issues/IJAKXJ
https://gitee.com/Guhuitao/resjtrhtr/issues/IJAKY2
https://gitee.com/Guhuitao/resjtrhtr/issues/IJAKYJ
https://gitee.com/Guhuitao/resjtrhtr/issues/IJAKZ1
https://gitee.com/Guhuitao/resjtrhtr/issues/IJAKZP
https://gitee.com/Guhuitao/resjtrhtr/issues/IJAL05
https://gitee.com/Guhuitao/resjtrhtr/issues/IJAL0J
https://gitee.com/Guhuitao/resjtrhtr/issues/IJAL0V
https://gitee.com/Guhuitao/resjtrhtr/issues/IJAL17
https://gitee.com/Guhuitao/resjtrhtr/issues/IJAL1H
https://gitee.com/Guhuitao/resjtrhtr/issues/IJAL1X
https://gitee.com/Guhuitao/resjtrhtr/issues/IJAL2A
https://gitee.com/Guhuitao/resjtrhtr/issues/IJAL2I
https://gitee.com/Guhuitao/resjtrhtr/issues/IJAL2R

以上代码均使用递归实现,符合递归、搜索与回溯的主题。在实际应用中,注意链表节点定义的正确性,并处理边界情况如空链表。

http://www.jsqmd.com/news/668996/

相关文章:

  • 告别付费内网穿透!用Docker 5分钟搞定PPTP服务器,实现免费不限端口的内网访问
  • 2026年招远舞蹈机构TOP5盘点:谁才是口碑与教学双赢的选择?
  • 告别手动点按!用Auto.js的Shell命令5分钟搞定微信/QQ自动化跳转(附am/pm命令详解)
  • 2026奇点大会唯一未删减技术圆桌实录(含OpenAI、Ethereum基金会、中科院自动化所三方闭门共识):AGI主权归属的区块链终局方案
  • C语言编译器app
  • C++函数模板:OOP中的万能利器
  • AI Agent Harness Engineering 产品设计指南:如何平衡用户体验与技术可行性?
  • 【AGI决策能力评估权威框架】:2024全球7大实验室实测数据+3层可验证指标体系首次公开
  • 引用,浅拷贝,深拷贝
  • 避开这些坑,你的Android设备才能顺利通过Google认证:XTS测试环境与版本配置指南
  • C语言中常用“计时“方法总结
  • 编排者的时代:从单兵工具到群体智能的认知跃迁
  • 调试LVDS屏别再只改代码了!从屏闪、白屏到触屏漂移,三个实战问题背后的硬件时序原理
  • MATLAB App打包 vs exe打包:我该选哪个?一次讲清两者的区别与适用场景
  • 别再傻傻分不清!用一杯水和一把尺子,5分钟搞懂ADC的LSB与精度
  • 自建 code-server vs CloudStudio:为什么插件不能用?
  • 2026年贵阳AI营销招聘生态全景:从传统销售到智能获客的职业进阶指南 - 精选优质企业推荐官
  • 图像擦除算法研究
  • 平衡二叉树的奥秘:AVLTree高效实现解析
  • 【2024 AGI前沿突破】:斯坦福+DeepMind联合验证的4类自主学习范式对比报告
  • 驾驭 Claude 的智能(Harnessing Claude’s intelligence)
  • 贵阳毕业季求职指南|1200万毕业生涌入,这5类岗位和6家公司最值得关注 - 精选优质企业推荐官
  • TypeScript的template literal types实现SQL查询的类型安全
  • 【AGI军事伦理红区预警】:20年国防科技专家首次公开3大不可逾越的AI作战红线
  • 划时代claude-opus-4-7重磅来袭,DMXAPI平台特惠开放,降低 AI 使用成本
  • NLP学习笔记13:BERT系列模型——从预训练到 RoBERTa 与 ALBERT
  • CREO实战宝典:从阵列到骨架模型,解锁十大经典零件设计全流程(曲柱、风扇叶、齿轮参数化、油缸等)
  • 告别DrawCall卡顿!Unity 2022最新Sprite Atlas图集打包保姆级教程(含旧版本迁移指南)
  • 鸣潮自动化终极指南:如何用ok-ww实现智能自动战斗与资源收集
  • 2026年,泉州创业者资源对接会哪个好用?