/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* reverseList(ListNode* head) {
if(!head) return head;
ListNode*pre=nullptr;
ListNode*cur=head;
ListNode*next=nullptr;
while(cur!=nullptr){
next=cur->next;
cur->next=pre;
pre=cur;
cur=next;
}
return pre;
}
};
剑指 Offer 24. 反转链表
最新推荐文章于 2024-09-06 16:39:50 发布