首页 > 其他分享 >从尾到头打印链表

从尾到头打印链表

时间:2022-12-10 15:46:26浏览次数:35  
标签:head 到头 链表 vector 从尾 ans

输入一个链表的头结点,按照 从尾到头 的顺序返回节点的值。

返回的结果用数组存储。

class Solution {
public:
    vector<int> printListReversingly(ListNode* head) {
        vector<int> ans;
        for (auto p = head; p; p = p->next) ans.push_back(p->val);
        reverse (ans.begin(), ans.end());
        
        return ans;
    }
};

  

标签:head,到头,链表,vector,从尾,ans
From: https://www.cnblogs.com/leetothemoon/p/16971673.html

相关文章