从尾到头打印链表 剑指offer
题目描述
输入一个链表,从尾到头打印链表每个节点的值。
输入描述:
输入为链表的表头
输出描述:
输出为需要打印的“新链表”的表头
/** * struct ListNode { * int val; * struct ListNode *next; * ListNode(int x) : * val(x), next(NULL) { * } * }; */ class Solution { public: vector<int> printListFromTailToHead(struct ListNode* head) { std::stack<ListNode*> nodes; vector<int> result; ListNode* pNode = head; while(pNode != NULL){ nodes.push(pNode); pNode = pNode->next; } while(!nodes.empty()){ pNode = nodes.top(); result.push_back(pNode->val); nodes.pop(); } return result; } };
转载请注明出处:
C++博客园:godfrey_88
http://www.cnblogs.com/gaobaoru-articles/
posted on 2016-03-02 15:54 Brainer-Gao 阅读(109) 评论(0) 编辑 收藏 举报