牛客网4 从尾到头打印链表

解题思路:

使用栈(先进后出的想法)

/**
*  struct ListNode {
*        int val;
*        struct ListNode *next;
*        ListNode(int x) :
*              val(x), next(NULL) {
*        }
*  };
*/
class Solution {
public:
    vector<int> printListFromTailToHead(ListNode* head) {
        vector<int> ArrayList;
        stack<int> ArrayStack;
        ListNode* pNode=head;
        while(pNode!=nullptr)
        {
            ArrayStack.push(pNode->val);
            pNode=pNode->next;
        }
        while(!ArrayStack.empty()){
            ArrayList.push_back(ArrayStack.top());
            ArrayStack.pop();
        }
        return ArrayList;
        
    }
};
发布了109 篇原创文章 · 获赞 0 · 访问量 8880

猜你喜欢

转载自blog.csdn.net/qq_39029148/article/details/104018709
今日推荐