牛客-從尾到頭打印鏈表

牛客-從尾到頭打印鏈表

#include <stdio.h>
#include <queue>
#include <stack>
#include <math.h>
#include <map>
#include <string.h>
#include <string>
#include <cstring>
#include <set>
using namespace std;


struct ListNode {
    int val;
    struct ListNode *next;
    ListNode(int x) :
    val(x), next(NULL) {
    }
};



class Solution {
public:
    vector<int> printListFromTailToHead(ListNode* head) {
        stack<ListNode*> st1;
        vector<int> res;
        
        while (head!=NULL) {
            st1.push(head);
            head=head->next;
        }
        
        while (!st1.empty()) {
            ListNode* node = st1.top();
            st1.pop();
            res.push_back(node->val);
        }
        
        return res;
    }
};

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章