ソードはオファー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> s;
        vector<int> ans;
        while(head!=NULL)
        {
    
    
            s.push(head->val);
            head=head->next;
        }
        
        while(s.size())
        {
    
    
            ans.push_back(s.top());
            s.pop();
        }
        return ans;
    }
};

おすすめ

転載: blog.csdn.net/weixin_45260385/article/details/110097354