/**
* 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 (n==m) return head;
ListNode* front = NULL;
ListNode* cur = head;
ListNode* pre = NULL;
ListNode* temp = NULL;
ListNode* next = NULL;
for (int i=1; i<=n; i++){
if (i<m){
front = cur;
cur = cur->next;
}
else {
if (i == m) {
//pre = temp;
temp = cur;
}
next = cur->next;
cur->next = pre;
pre = cur;
cur = next;
}
}
temp->next = next;
if (front != NULL) front->next = pre;
else return pre;
return head;
}
};