JZ6 从尾到头打印链表

/**
*  struct ListNode {
*        int val;
*        struct ListNode *next;
*        ListNode(int x) :
*              val(x), next(NULL) {
*        }
*  };
*/
class Solution {
public:
    
    stack <int> a;
    vector<int> printListFromTailToHead(ListNode* head) {
        vector<int> vec;
        ListNode* current_p = head;
        if(head!=NULL){
            while(current_p -> next != NULL){
                a.push(current_p -> val);
                current_p = current_p -> next;
            }
            a.push(current_p -> val);

            int temp;
            while(!a.empty()){
                temp = a.top();
                a.pop();
                vec.push_back(temp);
            }
        }
        return vec;
        
    }
};

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务