剑指offer(C++)--反转链表

it2022-05-05  197

题目

输入一个链表,反转链表后,输出新链表的表头。 /* struct ListNode { int val; struct ListNode *next; ListNode(int x) : val(x), next(NULL) { } };*/ class Solution { public: ListNode* ReverseList(ListNode* pHead) { ListNode * pReverseHead = nullptr; ListNode * pNode = pHead; ListNode * pPre = nullptr; while(pNode != nullptr) { ListNode * pNext = pNode->next; if(pNext == nullptr) { pReverseHead = pNode; } pNode->next = pPre; pPre = pNode; pNode = pNext; } return pReverseHead; } };

最新回复(0)