/**
* 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 == nullptr || head->next == nullptr)
{
return head;
}
ListNode* pre = head;
ListNode* cur = pre->next;
ListNode* temp = cur->next;
pre->next = nullptr;
while(temp)
{
cur->next = pre;
pre = cur;
cur = temp;
temp = temp->next;
}
cur->next = pre;
return cur;
}
};
206. 反转链表
最新推荐文章于 2024-07-03 11:26:52 发布