题解 | #链表内指定区间反转#
链表内指定区间反转
https://www.nowcoder.com/practice/b58434e200a648c589ca2063f1faf58c
func reverseBetween( head *ListNode , m int , n int ) *ListNode { if m == n { return head } var pre, tail, begin *ListNode if m == 1 { begin = head tail = head }else{ pre = head for i:=1 ; i<m-1 ; i++ { pre = pre.Next } begin = pre.Next tail = pre.Next } var P *ListNode for i:=m ; i<n+1 ; i++ { mid := begin.Next begin.Next = P P = begin begin = mid } tail.Next = begin if m == 1 { head = P }else { pre.Next = P } return head }