【剑指offer】从尾到头打印链表
2016-04-09 11:32
435 查看
***************从尾到头打印链表***************************
思路1:借助栈结构来实现:
思路二“:递归实现(递归实现在链表非常长的时候会导致栈溢出的问题。)
思路1:借助栈结构来实现:
vector<int> printListFromTailToHead(struct ListNode* head) { stack<int> vestack; vector<int> ret; while (head != NULL) { vestack.push(head->val); head = head->next; } while (!vestack.empty()) { int val = vestack.top(); ret.push_back(val); vestack.pop(); } return ret; }
思路二“:递归实现(递归实现在链表非常长的时候会导致栈溢出的问题。)
vector<int> printListFromTailToHead(struct ListNode* head) { vector<int> ret; if (NULL != head) { if (NULL != head->next) { printListFromTailToHead(head->next); } ret.push_back(head->val); } return ret; }
相关文章推荐
- 【剑指offer】数值的整数次方
- javascript获取url参数
- $getJSON方法中文乱码
- Jquery笔记
- 【剑指offer】懂二进制
- js中将返回值变成json格式
- js中获取键盘事件
- 【剑指offer】二维数组中的查找
- LABVIEW转换JSON为数组和图表
- 1.javascript基础
- bootstrap 文本对齐风格
- 前端学习 开发编辑器 Atom使用配置
- bootstrap 强调相关的类
- 常规功能和模块自定义系统 (cfcmms)—040模块记录(数据)的权限设计(5)聚合操作
- node初探(很全的hello world工程)
- JS闭包以及具体题目1
- Ubuntu14.04+Caffe+CPU,挖挖坑坑
- 开发中js常用的格式化方法
- 页面JQuery日期转换
- bootstrap导航栏中的按钮