# 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 reverseBetween(self , head: ListNode, m: int, n: int) -> ListNode:
        # write code here
        res, res.next = ListNode(-1), head#设置哨兵节点,指向链表头,省去边界判断问题
        pre, cur = res, head

        for i in range(1,m):#找到第m个节点为cur
            pre, cur = cur, cur.next
        
        for i in range(m,n):#进行链表节点m~n的反转操作
            nxt = cur.next
            cur.next, nxt.next, pre.next = nxt.next, pre.next, nxt
        
        return res.next