# class ListNode:
#     def __init__(self, x):
#         self.val = x
#         self.next = None
#
# 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
#
# 
# @param head ListNode类 
# @param m int整型 
# @param n int整型 
# @return ListNode类
#
class Solution:
    def revers_list(self,head:ListNode,m:int,n:int)->List:
        if head is None:
            return []
        Phead=head
        lists=[]
        while Phead:
            lists.append(Phead.val)
            Phead=Phead.next
        list2=lists[0:m-1]+lists[m-1:n][::-1]+lists[n:len(lists)]
        return list2
    

    def reverseBetween(self , head: ListNode, m: int, n: int) -> ListNode:
        # write code here
        if head is None:
            return None
        lists=self.revers_list(head,m,n)
        Phead=head
        for i in lists:
            Phead.val=i
            Phead=Phead.next
        return head