从尾到头打印链表

 

 C++小白抄题解ing

/**
*  struct ListNode {
*        int val;
*        struct ListNode *next;
*        ListNode(int x) :
*              val(x), next(NULL) {
*        }
*  };
*/
class Solution {
public:
    vector<int> printListFromTailToHead(ListNode* head) {
       vector<int> ret;
        while(head){
            ret.push_back(head->val);
            head=head->next;
        }
        std::reverse(ret.begin(),ret.end());
        return ret;
    }
};

 

posted on 2020-12-16 10:20  Taurus20000519  阅读(27)  评论(0)    收藏  举报