剑指offer(3):从尾到头打印单链表

输入一个链表,从尾到头打印链表每个节点的值。

/**
*  struct ListNode {
*        int val;
*        struct ListNode *next;
*        ListNode(int x) :
*              val(x), next(NULL) 
*        }
*  };
*/
class Solution {
public:
    vector<int> printListFromTailToHead(ListNode* head) {
        vector<int> inverse;
        stack<int> node_values;
        
        ListNode * pNode = head;
        while(pNode != nullptr)
        {
            node_values.push(pNode->val);
            pNode = pNode->next;
        }
        
        while(!node_values.empty())
        {
            inverse.push_back(node_values.top());
            node_values.pop();
        }
        return inverse;
        
    }
};
原文地址:https://www.cnblogs.com/heifengli/p/8886346.html