反转链表

主要思路是定义两个指向结点的指针
/*
struct ListNode { int val; struct ListNode *next; ListNode(int x) : val(x), next(NULL) { } };*/ class Solution { public: ListNode* ReverseList(ListNode* pHead) { ListNode* pre=NULL; while(pHead!=NULL){ ListNode* Next=pHead->next; pHead->next=pre; pre=pHead; pHead=Next; } return pre; } };

 

posted @ 2021-06-03 22:48  qwer95  阅读(15)  评论(0)    收藏  举报