题目:
题解:
func reverseBetween(head *ListNode, left, right int) *ListNode {
// 设置 dummyNode 是这一类问题的一般做法
dummyNode := &ListNode{Val: -1}
dummyNode.Next = head
pre := dummyNode
for i := 0; i < left-1; i++ {
pre = pre.Next
}
cur := pre.Next
for i := 0; i < right-left; i++ {
next := cur.Next
cur.Next = next.Next
next.Next = pre.Next
pre.Next = next
}
return dummyNode.Next
}