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;
        
    }
};

全部评论

相关推荐

仁者伍敌:难怪小公司那么挑剔,让你们这些大佬把位置拿了
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务