/**
* struct ListNode {
* int val;
* struct ListNode *next;
* ListNode(int x) :
* val(x), next(NULL) {
* }
* };
*/
class Solution {
public:
vector<int> printListFromTailToHead(ListNode* head) {
std::stack<ListNode*>nodes;
ListNode* pNode=head;
while(pNode!=NULL){
nodes.push(pNode);
pNode=pNode->next;
}
vector<int> result;
while(!nodes.empty()){
pNode=nodes.top();
result.push_back(pNode->val);
nodes.pop();
}
return result;
}
};
从尾到头打印链表
最新推荐文章于 2022-02-15 14:24:11 发布