从尾到头打印链表

#include "stdafx.h"
#include <string>
using namespace std;
#include <vector>
#include <stack>

typedef struct tag_listnode
{
    int data;
    struct  tag_listnode *next;

}listnode;


class Solution
{
public:
    Solution();
    ~Solution();

    vector<int> reseveListnode(listnode* head)
    {
        listnode *node = head;
        stack <int>  st;
        vector <int> vec;

        while (node !=nullptr)
        {
            st.push(node->data);
            node = node->next;
        }

        while (!st.empty())
        {
            vec.push_back(st.top());
            st.pop();
        }

        return vec;

    }

private:

};

int main()
{
    listnode head, node1, node2, node3, node4;
    node1.data = 11;  node1.next = &node2;
    node2.data = 22; node2.next = &node3;
    node3.data = 33; node3.next = &node4;
    node4.data = 44; node4.next = nullptr;
    head.next = &node1;
    Solution sou;
    vector <int>vecRet;
    vecRet = sou.reseveListnode(&head);
    return 1;
}

天天向上
原文地址:https://www.cnblogs.com/hg07/p/12728051.html