输入一个链表,按链表值从尾到头的顺序返回一个ArrayList

/**
*  struct ListNode {
*        int val;
*        struct ListNode *next;
*        ListNode(int x) :
*              val(x), next(NULL) {
*        }
*  };
*/
class Solution {
public:
    vector<int> printListFromTailToHead(ListNode* head) {
        vector <int> value;
        stack<int> nodes;
        ListNode *pNode = head;
        while(pNode != nullptr)
        {
            nodes.push(pNode->val);
            pNode=pNode->next;
        }
        int len = nodes.size();
        while(!nodes.empty())
        {
            value.push_back(nodes.top());
            nodes.pop();
        }
        return value;
    }
};

猜你喜欢

转载自blog.csdn.net/u013187057/article/details/81175306