import java.util.*;

/*
 * public class ListNode {
 *   int val;
 *   ListNode next = null;
 * }
 */

public class Solution {
    /**
     * 
     * @param head ListNode类 
     * @param m int整型 
     * @param n int整型 
     * @return ListNode类
     */
    public ListNode reverseBetween (ListNode head, int m, int n) {
        // write code here
        if(m >= n) {
            return head;
        }
        int i = 1;
        ListNode p = head;
        int[] arr = new int[n - m + 1];
        while(p != null && i < m) {
            i++;
            p = p.next;
        }
        ListNode q = p;
        int j = 0;
        while(j < arr.length && q != null) {
            arr[j] = q.val;
            j++;
            q = q.next;
        }
        j = 0;
        while(j < arr.length && p != null) {
            p.val = arr[arr.length - 1 - j];
            j++;
            p = p.next;
        }
        return head;
    }
}