/**
* 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* reverseBetween(ListNode* head, int left, int right) {
ListNode* cur=head;
vector<int> v;
int k=1;
while(k!=left)
{
k++;cur=cur->next;
}
while(k<=right)
{
k++;v.push_back(cur->val);cur=cur->next;
}
cur=head;
reverse(v.begin(),v.end());
k=1;
while(k!=left)
{
k++;cur=cur->next;
}
for(int i=0;i<v.size();i++)
{
cur->val=v[i];
cur=cur->next;
}
return head;
}
};
LeetCode92. 反转链表 II
最新推荐文章于 2024-05-28 21:13:10 发布