从尾到头打印链表(栈)

来源:互联网 发布:钣金展开软件下载 编辑:程序博客网 时间:2024/06/06 06:36
classSolution {
public:
    vector<int> printListFromTailToHead(ListNode* head) {
    vector<int> result;
    std::stack<ListNode*> nodes;
 
    ListNode* pNode = head;
    while(pNode){
        nodes.push(pNode);
        pNode = pNode->next;
    }
 
    while(!nodes.empty()){
        pNode = nodes.top();
        result.push_back(pNode->val);
        nodes.pop();
    }
 
    returnresult;
}
};
原创粉丝点击