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

反转链表-C++

分享一个大牛的人工智能教程。零基础!通俗易懂!风趣幽默!希望你也加入到人工智能的队伍中来!请轻击人工智能教程https://www.captainai.net/troubleshooter

// 面试题24:反转链表 // 题目:定义一个函数,输入一个链表的头结点,反转该链表并输出反转后链表的 // 头结点。 #include <cstdio> #include <stdio.h> #include <stdlib.h> struct ListNode { int m_nValue; ListNode *m_pNext; }; ListNode *CreateListNode(int value) { ListNode *pNode = new ListNode(); pNode->m_nValue = value; pNode->m_pNext = nullptr; return pNode; } void ConnectListNodes(ListNode *pCurrent, ListNode *pNext) { if (pCurrent == nullptr) { printf("Error to connect two nodes.\n"); exit(1); } pCurrent->m_pNext = pNext; } void PrintListNode(ListNode *pNode) { if (pNode == nullptr) { printf("The node is nullptr\n"); } else { printf("The key in node is %d.\n", pNode->m_nValue); } } void PrintList(ListNode *pHead) { printf("PrintList starts.\n"); ListNode *pNode = pHead; while (pNode != nullptr) { printf("%d\t", pNode->m_nValue); pNode = pNode->m_pNext; } printf("\nPrintList ends.\n"); } void DestroyList(ListNode *pHead) { ListNode *pNode = pHead; while (pNode != nullptr) { pHead = pHead->m_pNext; delete pNode; pNode = pHead; } } void AddToTail(ListNode **pHead, int value) { ListNode *pNew = new ListNode(); pNew->m_nValue = value; pNew->m_pNext = nullptr; if (*pHead == nullptr) { *pHead = pNew; } else { ListNode *pNode = *pHead; while (pNode->m_pNext != nullptr) pNode = pNode->m_pNext; pNode->m_pNext = pNew; } } void RemoveNode(ListNode **pHead, int value) { if (pHead == nullptr || *pHead == nullptr) return; ListNode *pToBeDeleted = nullptr; if ((*pHead)->m_nValue == value) { pToBeDeleted = *pHead; *pHead = (*pHead)->m_pNext; } else { ListNode *pNode = *pHead; while (pNode->m_pNext != nullptr && pNode->m_pNext->m_nValue != value) pNode = pNode->m_pNext; if (pNode->m_pNext != nullptr && pNode->m_pNext->m_nValue == value) { pToBeDeleted = pNode->m_pNext; pNode->m_pNext = pNode->m_pNext->m_pNext; } } if (pToBeDeleted != nullptr) { delete pToBeDeleted; pToBeDeleted = nullptr; } } ListNode *ReverseList(ListNode *pHead) { ListNode *pReversedHead = nullptr; ListNode *pNode = pHead; ListNode *pPrev = nullptr; while (pNode != nullptr) { ListNode *pNext = pNode->m_pNext; if (pNext == nullptr) pReversedHead = pNode; pNode->m_pNext = pPrev; pPrev = pNode; pNode = pNext; } return pReversedHead; } // ====================测试代码==================== ListNode *Test(ListNode *pHead) { printf("The original list is: \n"); PrintList(pHead); ListNode *pReversedHead = ReverseList(pHead); printf("The reversed list is: \n"); PrintList(pReversedHead); return pReversedHead; } // 输入的链表有多个结点 void Test1() { ListNode *pNode1 = CreateListNode(1); ListNode *pNode2 = CreateListNode(2); ListNode *pNode3 = CreateListNode(3); ListNode *pNode4 = CreateListNode(4); ListNode *pNode5 = CreateListNode(5); ConnectListNodes(pNode1, pNode2); ConnectListNodes(pNode2, pNode3); ConnectListNodes(pNode3, pNode4); ConnectListNodes(pNode4, pNode5); ListNode *pReversedHead = Test(pNode1); DestroyList(pReversedHead); } // 输入的链表只有一个结点 void Test2() { ListNode *pNode1 = CreateListNode(1); ListNode *pReversedHead = Test(pNode1); DestroyList(pReversedHead); } // 输入空链表 void Test3() { Test(nullptr); } int main(int argc, char *argv[]) { Test1(); Test2(); Test3(); return 0; } // ------ Output ------ /* The original list is: PrintList starts. 1 2 3 4 5 PrintList ends. The reversed list is: PrintList starts. 5 4 3 2 1 PrintList ends. The original list is: PrintList starts. 1 PrintList ends. The reversed list is: PrintList starts. 1 PrintList ends. The original list is: PrintList starts. PrintList ends. The reversed list is: PrintList starts. PrintList ends. */
http://www.jsqmd.com/news/712119/

相关文章:

  • 浅谈现代物流中的自动化立体仓库毕业设计
  • VFP JSON处理利器nfJson:纯代码实现、高性能解析与实战应用
  • TypeScript Go终极指南:如何快速掌握TypeScript原生移植技术
  • docker-compose安装
  • 彻底解决Prisma事务超时:Node进程崩溃的终极指南
  • 深度学习优化:学习率调度与早停
  • 从‘乱码’到‘清晰’:深入理解JavaScript中Base64编码的字符集‘暗礁’与安全实践
  • 告别组件绑定困境:Dapr插件架构如何重塑云原生扩展能力
  • 2026液压家用电梯技术分享:山东别墅电梯、山东家用电梯、螺杆电梯、观光电梯、三层电梯、二层电梯、室内电梯、室外电梯选择指南 - 优质品牌商家
  • JCSprout算法优化:空间换时间策略的终极指南
  • FLASH Viterbi算法:动态规划与并行计算的优化实践
  • Rust持久化内存编程:使用persistent-memory库构建崩溃安全的B+树索引
  • 2026年3月零损耗限流装置厂商推荐,深度零损耗限流装置/零损耗限流装置,零损耗限流装置定制厂家有哪些 - 品牌推荐师
  • SPF扁平化失败原因与解决方案全解析
  • PPO算法原理与Docker构建优化实践
  • 终极指南:如何优雅解决Viper配置合并冲突,轻松处理多源数据冲突
  • 终极指南:Foundation Sites生态系统探索—第三方插件与扩展资源大全
  • 发廊专用热水器厂家精选|2026年高性价比发廊热水器厂家汇总与推荐:沐酷智能电器领衔 - 栗子测评
  • 超轻量容器革命:用Distroless构建前后端分离Web应用的最佳实践指南
  • 革命性突破:lottie-web动画断点续播实现终极指南
  • 如何在5分钟内用Revelation光影包让Minecraft画面达到电影级效果
  • 简历写“会用 AI“,含金量正在分化
  • 2026 年热门的江苏涂装厂家推荐:靠谱喷涂厂家哪家好、注塑厂家推荐 - 栗子测评
  • 终极指南:如何从OpenCensus平滑迁移到OpenTelemetry,彻底告别性能瓶颈
  • DoRA技术在大模型嵌入层高效微调中的应用
  • 生成数学解释信息图-好事多磨
  • 如何将Foundation-Sites与Svelte集成:释放编译时框架的终极性能优势
  • PostCSS类型定义:完整的TypeScript支持与类型安全指南
  • 云计算成本优化:AI训练任务中的六大技术维度解析
  • 告别代码臃肿:Professional Programming教你用简洁设计征服复杂性