题解 | #链表内指定区间反转#
链表内指定区间反转
http://www.nowcoder.com/practice/b58434e200a648c589ca2063f1faf58c
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 {
// write code here
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
}