面试题:从尾到头打印链表
2016-10-21 16:30
204 查看
题目:输入一个链表的头结点,从尾到头反过来打印出每个节点的值。
方法1:使用栈
方法2:递归
方法1:使用栈
/* 链表节点定义如下: struct ListNode { int _data; ListNode* _next; }; */ void PrintListTailToHead(ListNode* phead) { assert(phead); stack<ListNode*> s; ListNode* cur = phead; while (cur) { s.push(cur); cur = cur->_next; } while (!s.empty()) { cout<<s.top()->_data<<" "; s.pop(); } cout<<endl; }
方法2:递归
void PrintListTailToHead(ListNode* phead) { if (phead == NULL) { return; } PrintListTailToHead(phead->_next); cout<<phead->_data<<" "; }
相关文章推荐
- 剑指Offer 面试题5 从尾到头打印链表
- 剑指Offer:面试题5——从尾到头打印链表(java实现)
- 剑指offer面试题5:从尾到头打印链表
- 剑指offer--面试题5:从尾到头打印链表
- 【剑指offer】面试题5:从尾到头打印链表 java
- 面试题五: 从尾到头打印链表
- 剑指Offer: 面试题5 从尾到头打印链表
- 面试题(六)从尾到头打印链表
- 剑指offer|面试题5:从尾到头打印链表(Java实现)
- 面试题5:从尾到头打印链表
- 剑指offer面试题[5]-从尾到头打印链表
- 剑指Offer---面试题5:从尾到头打印链表
- 剑指offer - 面试题5:从尾到头打印链表
- 面试题之【从尾到头打印链表】
- 【面试题5】从尾到头打印链表
- 剑指Offer_面试题05_从尾到头打印链表
- 面试题5:从尾到头打印链表
- 面试题6:从尾到头打印链表(offer)
- 经典面试题——链表从尾到头打印
- (剑指Offer)面试题5:从尾到头打印链表