
转载请注明, 原文地址:反转链表
输入一个链表,反转链表后,输出新链表的表头。
ListNode* ReverseList(ListNode* pHead) {
vector<ListNode*> res;
ListNode* temp=nullptr;
res.push_back(temp);
while(pHead){
res.push_back(pHead);
pHead=pHead->next;
}
for(int i=res.size()-1;i>0;--i){
res[i]->next=res[i-1];
}
return res[res.size()-1];
}
转载请注明:
转载自YuLai's Blog,原文地址:反转链表
发表评论
沙发空缺中,还不快抢~