/**
* 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||!head->next) return head;
auto i=head,j=head->next;
while(j){
auto t=j->next;
j->next=i;
i=j;
j=t;
}
head->next=nullptr;
return i;
}
};
LeetCode 206. 反转链表
最新推荐文章于 2024-07-22 16:27:42 发布