class Solution {
public:
ListNode reverseBetween(ListNode head, int m, int n) {
if(!head || !head->next || m==n)
return head;
ListNode dummy = new ListNode(0);
dummy->next = head;
ListNode cur = dummy;
for(int i = 0; i < m-1; ++i){
cur = cur->next;
}
ListNode* tail1 = cur;
cur = cur->next;
ListNode* tail2 = cur;
ListNode* tmp1 = cur->next;
ListNode* tmp2 = tmp1->next;
for(int i = 0; i < n-m; ++i){
tmp1->next = cur;
cur = tmp1;
tmp1 = tmp2;
if(tmp2 == NULL)
break;
tmp2 = tmp2->next;
}
tail1->next = cur;
tail2->next = tmp1;
return dummy->next;
}
};