题解 | #链表内指定区间反转#
链表内指定区间反转
http://www.nowcoder.com/practice/b58434e200a648c589ca2063f1faf58c
package main import . "nc_tools" //头插法 func reverseBetween( head *ListNode , m int , n int ) *ListNode { // write code here if head == nil || head.Next == nil { return head } dummy := &ListNode{Next : head} pre := dummy for i := 1; i < m; i++ { pre = pre.Next } cur := pre.Next for i := m; i < n; i++ { temp := cur.Next cur.Next = temp.Next temp.Next = pre.Next pre.Next = temp } return dummy.Next }