输入一个链表,从尾到头打印链表每个节点的值。
/**
* struct ListNode {
* int val;
* struct ListNode *next;
* ListNode(int x) :
* val(x), next(NULL) {
* }
* };
*/
class Solution {
public:
vector<int> printListFromTailToHead(struct ListNode* head) {
vector<int> res;
printListReversingly_Recursively(head,res);
return res;
}
private:
void printListReversingly_Recursively(struct ListNode* head,vector<int>& res){
if(head!=NULL){
if(head->next!=NULL)
printListReversingly_Recursively(head->next,res);
res.push_back(head->val);
}
}
};