リンクリストC ++の逆順を実現

問題の説明

リンクリストを入力し、リンクリストの最後から最初の順序でArrayListを返します。

入る

{
    
    67,0,24,58}

戻り値

[58,24,0,67]
#include<iostream>
using namesapce std;
struct ListNode {
    
    
        int val;
        struct ListNode *next;
        ListNode(int x) :
             val(x), next(NULL) {
    
    
        }
 };

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

おすすめ

転載: blog.csdn.net/p715306030/article/details/114712794