class Solution {
public ListNode reverseBetween(ListNode head, int left, int right) {
ListNode dummyHead = new ListNode(0);
dummyHead.next = head;
ListNode g = dummyHead;
ListNode p = dummyHead;
for(int i =0;i<left-1;i++){
g = g.next;
p = p.next;
}
p = p.next;
for(int i = left;i < right;i++){
// 将p的next缓存一下
ListNode removed = p.next;
// p一直在left节点上,指向next.next
p.next = p.next.next;
// 将缓存值头插到g.next
removed.next = g.next;
// 将缓存值尾插到g
g.next = removed;
}
return dummyHead.next;
}
}