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

从尾到头打印链表

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;
    }
};

全部评论

相关推荐

“校招”、“3-5年经验”
xiaolihuam...:逆向工程不是搞外挂的吗,好像现在大学生坐牢最多的就是诈骗罪和非法侵入计算机系统罪,发美金,还居家办公,就是怕被一锅端,
点赞 评论 收藏
分享
06-25 16:00
武汉大学 Java
工科研究生底薪工资就开3k啊??
机械打工仔:写文章提成的岗位工资低,你怪工科?
点赞 评论 收藏
分享
点赞 评论 收藏
分享
05-22 12:44
已编辑
门头沟学院 golang
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

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