package main
import . "nc_tools"
/*
 * type ListNode struct{
 *   Val int
 *   Next *ListNode
 * }
 */

/**
  * 
  * @param head ListNode类 
  * @param m int整型 
  * @param n int整型 
  * @return ListNode类
*/
func reverseBetween( head *ListNode ,  m int ,  n int ) *ListNode {
    if m == n {
        return head
    }
    dumny := &ListNode{Next:head}
    p := dumny
    i := 0
    for i < m-1{
        i++
        p = p.Next
    }
    
    var next *ListNode
    var f func(head *ListNode,m int,n int) *ListNode
    f = func(head *ListNode,m int,n int) *ListNode {
        if n == m {
            next = head.Next
            return head
        }
        
        last := f(head.Next,m,n-1)
        head.Next.Next = head
        head.Next = next
        return last
    }
    
    p.Next = f(p.Next,m,n)
    
    return dumny.Next
}