剑指Offer总结——从尾到头打印链表

class Solution {
public:
    vector<int> printListFromTailToHead(ListNode* head) {
        if(head == nullptr) {
            return {};
        }
        vector<int> res;
        helper(res, head);
        return res;
    }
    void helper(vector<int>& res, ListNode* head) {
        if(head->next == nullptr) {
            res.push_back(head->val);
            return;
        }
        helper(res, head->next);
        res.push_back(head->val);
    }
};

题目描述:
输入一个链表,按链表从尾到头的顺序返回一个ArrayList。

思路:递归

posted @ 2020-03-04 00:00  夜溅樱  阅读(104)  评论(0编辑  收藏  举报