您的位置:首页 > 其它

从尾到头打印链表

2015-10-30 20:50 323 查看

题目描述

输入一个链表,从尾到头打印链表每个节点的值。

/**
*  struct ListNode {
*        int val;
*        struct ListNode *next;
*        ListNode(int x) :
*              val(x), next(NULL) {
*        }
*  };
*/
class Solution {
public:
vector<int> printListFromTailToHead(struct ListNode* head) {

stack<int> stack;
vector<int> vector;
struct ListNode *p = head;
while(p != NULL) {
stack.push(p->val);
p=p->next;
}
while(!stack.empty()) {
vector.push_back(stack.top());
stack.pop();
}

return vector;
}

};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: