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

2020-03-03  本文已影响0人  Hubhub

题目描述

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<int> reversePrint(ListNode* head) {
        stack<int> st;
        while(head){
            st.push(head->val);
            head=head->next;
        }
        vector<int> ans;
        while(!st.empty()){
            ans.push_back(st.top());
            st.pop();
        }
        return ans;
    }
};
上一篇下一篇

猜你喜欢

热点阅读