#include<iostream>
#include<list>
using namespace std;
struct ListNode
{
int data;
ListNode* next;
};
ListNode* ReverseList(ListNode* pHead)
{
ListNode* NewHead=nullptr;
ListNode* pNext;
ListNode* pPre=nullptr;
ListNode* pCur = pHead;
while (pCur != nullptr){
pNext = pCur->next;//指针的成员函数只能用->
if (pNext == nullptr)
NewHead = pCur;
pCur->next = pPre;
pPre = pCur;
pCur = pNext;
}
return NewHead;
}
注意:在改变一个值之前,确保已经另行存储