剑指offer--18.从尾到头打印链表

递归,逐个加到后面
------------------------------------------------------------------------------
时间限制:1秒 空间限制:32768K 热度指数:798693
本题知识点: 链表

题目描述

输入一个链表,按链表值从尾到头的顺序返回一个ArrayList。
/**
*  struct ListNode {
*        int val;
*        struct ListNode *next;
*        ListNode(int x) :
*              val(x), next(NULL) {
*        }
*  };
*/
class Solution {
public:
    vector<int> printListFromTailToHead(ListNode* head) {
        vector<int> vec;
        if(head != NULL) {
            if(head->next != NULL) {
                vec = printListFromTailToHead(head->next);
            }
            vec.push_back(head->val);
        }
        return vec;
    }
};

 

posted @ 2019-03-28 16:19  朤尧  阅读(213)  评论(0编辑  收藏  举报