题解 | #从尾到头打印链表#

从尾到头打印链表

https://www.nowcoder.com/practice/d0267f7f55b3412ba93bd35cfa8e8035

#include <algorithm>
#include <iterator>
#include <vector>
class Solution {
  public:
    vector<int> printListFromTailToHead(ListNode* head) {
        vector<int>temp;
        if(head==nullptr)
            return {};
        ListNode* current=head;
        int length=0;
        while(current!=nullptr)
        {
            temp.push_back(current->val);
            length++;
            current=current->next;
        }
        //用这种做法,仅用了4字节的数据,就避免了在copy时反复给result数组扩容。
        vector<int>result(length);
        // std::reverse_copy(temp.begin(), temp.end(), std::back_inserter(result));
        std::reverse_copy(temp.begin(),temp.end(),result.begin());
        return result;
    }
};

全部评论

相关推荐

12-24 20:49
武汉大学 Java
点赞 评论 收藏
分享
秋招投简历提醒助手:个人经验是,一般面二十场左右就会进入侃侃而谈阶段。我今年七月末的时候开始的第一次面试,都是很多不会,回复很慢。后面慢慢迭代,到九月中的时候基本上面啥说啥,很放松的状态
远程面试的尴尬瞬间
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

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