/**
* 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 *dummy =new ListNode(-1);
dummy->next = head;
ListNode *prev = dummy;
for(int i = 0; i < left - 1; i ++) prev = prev->next;
ListNode *l = prev->next;
ListNode *r = l->next;
for(int i = 0; i < (right - left); i ++){
ListNode *next = r->next;
r->next = l;
l = r, r = next;
}
prev->next->next = r;
prev->next = l;
return dummy->next;
}
};