/**
* struct ListNode {
* int val;
* struct ListNode *next;
* ListNode(int x) : val(x), next(nullptr) {}
* };
*/
class Solution {
public:
ListNode* reverseBetween(ListNode* head, int m, int n) {
ListNode* dummy = new ListNode(0);
dummy->next = head;
ListNode* p = dummy;
for (int i = 0; i < m - 1; i++) {
p = p->next;
}
ListNode* cur = p->next;
ListNode* pre = nullptr;
for (int i = 0; i < n - m + 1; i++) {
ListNode* nxt = cur->next;
cur->next = pre;
pre = cur;
cur = nxt;
}
p->next->next = cur;
p->next = pre;
return dummy->next;
}
};