/**
* 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) return nullptr;
ListNode *next = head->next;
ListNode *last = nullptr;
while(next) {
head->next = last;
last = head;
head = next;
next = head->next;
}
head->next = last;
return head;
}
};
【链表】【指针】leecode.206. 反转链表
最新推荐文章于 2024-02-26 22:48:21 发布