/**
* 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)
return head;
vector<ListNode*>store;
ListNode*t=head;
while(t)
{
store.push_back(t);
t=t->next;
}
reverse(store.begin(),store.end());
for(int i=0;i<store.size();i++)
store[i]->next=nullptr;
for(int i=0;i<store.size()-1;i++)
store[i]->next=store[i+1];
return store[0];
}
};
leetcode 202 快乐数
2024-01-06 00:28:03 27 阅读