题解 | #反转链表#

/* struct ListNode { int val; struct ListNode next; ListNode(int x) : val(x), next(NULL) { } };/ class Solution { public: ListNode* ReverseList(ListNode* pHead) { if(!pHead) return NULL; else{ vector <ListNode*> Container; while (pHead) { Container.push_back(pHead); pHead=pHead->next; } reverse(Container.begin(),Container.end()); ListNode* newhead = Container[0]; ListNode* cur= newhead; for (int i=1;i<Container.size();i++) { cur->next=Container[i];//注意这里为地址而不是地址的地址,此步重新定义next指向 cur=cur->next;
} cur->next=nullptr; return newhead; } } };

全部评论

相关推荐

ResourceUt...:楼主有自己的垃圾箱,公司也有自己的人才库
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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