leetcode92: https://leetcode-cn.com/problems/reverse-linked-list-ii/
/**
* 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 m, int n)
{
ListNode dummy(-1);
dummy.next = head;
ListNode* prevNode = &dummy;
for(int i = 1;i<m;i++)
{
prevNode = prevNode->next;
}
ListNode* cur = prevNode->next;
for(int i = m;i<n;i++)
{
ListNode* next = cur->next;
cur->next = next->next;
next->next = prevNode->next;
prevNode->next = next;
}
return dummy.next;
}
};