剑指offer 面试题06. 从尾到头打印链表

题目描述

https://leetcode-cn.com/problems/cong-wei-dao-tou-da-yin-lian-biao-lcof/

参考

复杂度

时间:n
空间:n

代码

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

你可能感兴趣的:(剑指offer 面试题06. 从尾到头打印链表)