/**
 * struct ListNode {
 *	int val;
 *	struct ListNode *next;
 *	ListNode(int x) : val(x), next(nullptr) {}
 * };
 */
class Solution {
public:
    /**
     * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
     *
     * 
     * @param head ListNode类 
     * @param left int整型 
     * @param right int整型 
     * @return ListNode类
     */
    ListNode* reverseBetween(ListNode* head, int left, int right) {
        // write code here
        ListNode* ans = new ListNode(-1);
        ans->next = head;
        ListNode* temp = ans;

        int t = 1;
        while(t<left)
        {
            temp = temp->next;
            ++t;
        }

        ListNode* next = nullptr;
        // left的前一位;
        ListNode* pre = temp->next;
        while(t<=right)
        {
            ListNode* temp_2 = pre->next;
            pre->next = next;
            next = pre;
            pre = temp_2;
            ++t;
        }

        temp->next = next;
        while(next->next)
            next = next->next;

        next->next = pre;

        return ans->next;        
    }
};