题解 | #反转链表#
/*
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;
}
}
};