美文网首页
剑指offer 面试题06. 从尾到头打印链表

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

作者: Hubhub | 来源:发表于2020-03-03 22:20 被阅读0次

题目描述

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;
    }
};

相关文章

网友评论

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

      本文链接:https://www.haomeiwen.com/subject/pytrlhtx.html