0
点赞
收藏
分享

微信扫一扫

剑指 Offer 06. 从尾到头打印链表

思路:直接遍历 然后存储,简单。

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    vector<int> reversePrint(ListNode* head) {
        stack<int> a;
        while(head!=NULL){
            a.push((*head).val);
            head=(*head).next;
        }
        vector<int> as;
        while(a.size()){
            as.push_back(a.top());
            a.pop();
        }
        return as;
    }
};

 

举报

相关推荐

0 条评论