题目描述: 
 输入一个链表,从尾到头打印链表每个节点的值。
输入描述: 
 输入为链表的表头
输出描述: 
 输出为需要打印的“新链表”的表头
/** * struct ListNode { * int val; * struct ListNode *next; * ListNode(int x) : * val(x), next(NULL) { * } * }; */
class Solution {
public:
    vector<int> printListFromTailToHead(struct ListNode* head) {
        std::stack<ListNode*> nodes;
        vector<int> result;
        ListNode* pNode = head;
        while(pNode != NULL){
            nodes.push(pNode);
            pNode = pNode->next;
        }
        while(!nodes.empty()){
            pNode = nodes.top();
            result.push_back(pNode->val);
            nodes.pop();
        }
        return result;
    }
};
  如有建议或其他问题,可随时给我们留言。或者到以下链接:
Star/Fork/Push 您的代码,开源仓库需要您的贡献。 
 请查看Coding 题目网址和收藏Accepted代码仓库,进行coding!!!

京公网安备 11010502036488号