/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* reverseBetween(ListNode* head, int m, int n) {
if(head==nullptr || head->next==nullptr)
return head;
ListNode* dummy=new ListNode(-1);
dummy->next=head;
ListNode* reverseHead;
ListNode* pre,*cur;
cur=dummy;
while(m!=0){
--n;
--m;
pre=cur;
cur=cur->next;
}
reverseHead=pre;
head=cur->next;
while(n!=0){
--n;
pre=cur;
cur=head;
head=head->next;
cur->next=pre;
}
reverseHead->next->next=head;
reverseHead->next=cur;
return dummy->next;
}
};