
class Solution {
public:
ListNode* reverseBetween(ListNode* head, int m, int n) {
if(!head || !head->next) return head;
ListNode* first=new ListNode(0);
first->next=head;
int cnt=1;
while(cnt<m){
first=first->next;
++cnt;
}
ListNode* old_pre=first;
ListNode* cur=first->next;
ListNode* cur_next=cur->next;
ListNode* new_pre=cur;
ListNode* cur_next_next;
while(cur && cnt<n)
{
cur_next_next=cur_next->next;
cur_next->next=cur;
cur=cur_next;
cur_next=cur_next_next;
++cnt;
}
old_pre->next=cur;
new_pre->next=cur_next;
return m==1?cur:head;
}
};