题目描述

解题思路
 遍历链表,依次将元素压入栈中。然后依次弹出栈顶元素,存入数组返回。
 
程序
class Solution {
public:
    vector<int> reversePrint(ListNode* head) {
          ListNode *p=head;
          stack<int> s1;
          while(p!=NULL)  //遍历链表,元素依次入栈
          {
              s1.push(p->val); 
              p=p->next;  
          }
          int size=s1.size();
          vector<int> b;
          for(int i=size;i>0;i--) //依次弹出栈顶元素,存入数组。
          {
              b.push_back(s1.top());
              s1.pop();
          }
          return b;
          
    }
};
 
运行结果



















