/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode() : val(0), next(nullptr) {}
* ListNode(int x) : val(x), next(nullptr) {}
* ListNode(int x, ListNode *next) : val(x), next(next) {}
* };
*/
class Solution {
public:
ListNode* reverseList(ListNode* head) {
if (head == NULL)
return NULL;
ListNode* pHis = head;
ListNode* pCur = pHis->next;
pHis->next = NULL;
ListNode* pNext = NULL;
while (pCur != NULL) {
pNext = pCur->next;
pCur->next = pHis;
if (pNext == NULL) {
return pCur;
}
pHis = pCur;
pCur = pNext;
}
return pHis;
}
};
单链表翻转
最新推荐文章于 2024-08-09 15:03:58 发布