3个指针,pre,cur,nex,改变指针方向之前先保存nex,再将cur->next=pre;接着pre 和 cur 后移,最后cur==null的时候pre是 链表的头结点,因此返回pre即可。
/* 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 pHead; ListNode* pre=NULL; ListNode* cur=pHead; ListNode* nex=NULL; while(cur!=NULL) { nex=cur->next; cur->next=pre; pre=cur; cur=nex; } return pre; } };