从尾到头打印链表

题目描述

输入一个链表,从尾到头打印链表每个节点的值。
 1 /**
 2 *  struct ListNode {
 3 *        int val;
 4 *        struct ListNode *next;
 5 *        ListNode(int x) :
 6 *              val(x), next(NULL) {
 7 *        }
 8 *  };
 9 */
10 class Solution {
11 public:
12     vector<int> printListFromTailToHead(struct ListNode* head) {
13     
14         stack<int> stack;
15          vector<int> vector;
16         struct ListNode *p = head;
17         while(p != NULL) {
18              stack.push(p->val);
19              p=p->next;
20             }
21             while(!stack.empty()) {
22                 vector.push_back(stack.top());
23                 stack.pop();
24             }
25     
26         return vector;
27     }
28     
29          
30 };

 

posted @ 2015-10-30 20:50  疯狂的癫子  阅读(107)  评论(0编辑  收藏  举报