1、保存下一个节点防止链表断裂
2、保存上一个节点,反转链表
class Solution {
public:
ListNode* ReverseList(ListNode* pHead) {
if (NULL == pHead)
return NULL;
ListNode* pNode = pHead; //当前指针 往下走
ListNode* pResHead =NULL ; //反转之后的头指针
ListNode* pPre = NULL;//前一个指针
while (NULL != pNode)
{
ListNode* pNext = pNode->next; //保存下一个节点,防止链表断裂
if (NULL == pNext) //尾节点为新链表的头结点
pResHead = pNode;
pNode->next = pPre;
pPre = pNode;
pNode = pNext;
}
return pResHead;
}
};