面试题5 从尾到头打印链表

题目描述:

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

 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         if (head != NULL){
14             if (head->next != NULL){
15                 printListFromTailToHead(head->next);
16             }
17             v.push_back(head->val);
18         }
19         return v;
20     }
21     vector<int> v;
22 };