题目描述
输入一个链表,反转链表后,输出新链表的表头。
class Solution {
public:
ListNode* ReverseList(ListNode* pHead) {
if(pHead==NULL)return NULL;
ListNode* pre = NULL; //pre为当前节点的前一节点
ListNode* next = NULL; //next为当前节点的下一节
while(pHead!=NULL){
next=pHead->next; //先用next保存head的下一个节点的信息
pHead->next=pre; //改变当前结点的指向
//让pre,head依次向后移动一个节点,
pre=pHead;
pHead=next;
}
return pre;
}
};