剑指:反转链表
来源:互联网 发布:linux复制命令 编辑:程序博客网 时间:2024/05/02 04:37
题目描述
输入一个链表,反转链表后,输出链表的所有元素。
思路1:三个指针在链表上同时滑动,比较容易想到但是编码略复杂
ListNode* ReverseList(ListNode* pHead) { if (pHead == NULL) return NULL; if (pHead->next == NULL) return pHead; ListNode *pBefore=pHead, *p=pBefore->next, *pAfter=p->next; pHead->next=NULL; while(pAfter!=NULL) { p->next=pBefore; pBefore=p; p=pAfter; pAfter=pAfter->next; } p->next=pBefore; return p; }
思路2:依次取出链表的插入到新链表的头部
ListNode* ReverseList(ListNode* pHead) { if(pHead==NULL) return NULL; ListNode *newHead=pHead,*currentNode=pHead->next; newHead->next=NULL; while(currentNode!=NULL) { ListNode *t=currentNode->next; currentNode->next=newHead; newHead=currentNode; currentNode=t; } pHead=NULL; return newHead; }
0 0
- 剑指:反转链表
- 剑指offer:反转链表
- 剑指offer16--反转链表
- 剑指offer--反转链表
- 剑指offer--反转链表
- 剑指off-反转链表
- 剑指Offer-反转链表
- 《剑指offer》反转链表
- 剑指 offer:反转链表
- 剑指offer-反转链表
- 剑指offer 反转链表
- 剑指offer-反转链表
- 剑指offer:反转链表
- [剑指offer]反转链表
- 《剑指offer》-反转链表
- 剑指Offer:反转链表
- 剑指offer----反转链表
- 剑指offer|反转链表
- UIDocumentInteractionController 文档交互控制器 pdf 游览
- 使用夜神模拟器调试HBuilder Android项目
- C# 单元测试EntityFramework中的DbFunctions中的函数
- VR系列——Oculus Publishing文档:七、常见问题
- C语言中的操作符优先级
- 剑指:反转链表
- 给校园招聘中应聘嵌入式程序员的几点建议
- socket通信 简单的客户端和服务器的通信 服务器的代码
- 前端模块化方案的比较(webpack/require.js/seajs/browserify
- Mybatis-Spring 的高级配置 (SqlSessionFactoryBean)
- Leetcode 238 - Product of Array Except Self(递推)
- unable to access 'https://github.com/CocoaPods/Specs.git/': SSLRead() return error -9806
- Python入门(一):整数、浮点数和布尔值的运算
- 如何监控一个Activity加载完毕(完成渲染)-onWindowFocusChanged