从尾到头打印链表

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

开始想实现一个双向链表把输入记录下来,之后看到别人的才发现还可以用递归的方法来做。学到了。

原文地址:https://www.cnblogs.com/zl1991/p/4756297.html