/*
struct ListNode {
int val;
struct ListNode *next;
ListNode(int x) :
val(x), next(NULL) {
}
};*/
class Solution {
public:
ListNode* ReverseList(ListNode* pHead) {
if (pHead == NULL)
{
return NULL;
}
vector<ListNode* > v;
ListNode* temp = pHead;
while (temp != NULL)
{
v.push_back(temp);
temp = temp->next;
}
vector<ListNode* >::iterator it;
for (it = v.end()-1; it != v.begin(); it--)
{
(*it)->next = *(it-1);
}
(*it)->next = NULL;
it = v.end()-1;
return *it;
}
};
反转链表
最新推荐文章于 2025-03-12 20:11:42 发布