import java.util.*;
/*
* public class ListNode {
* int val;
* ListNode next = null;
* }
*/
public class Solution {
/**
*
* @param head ListNode类
* @param m int整型
* @param n int整型
* @return ListNode类
*/
public ListNode reverseBetween (ListNode head, int m, int n) {
// write code here
//哑巴节点减少边际问题
ListNode dummy = new ListNode(-1);
dummy.next = head;
ListNode pre = dummy;
//走到节点m-1处
for(int i = 0;i<m-1;i++){
pre = pre.next;
}
ListNode right = pre;
//走到节点n处
for(int j = 0;j<n-m+1;j++){
right = right.next;
}
//截取
ListNode left = pre.next;
ListNode cur = right.next;
//断开连接
pre.next = null;
right.next = null;
//反转
reverse(left);
//连接
pre.next = right;
left.next = cur;
return dummy.next;
}
public ListNode reverse(ListNode head){
ListNode pre = null;
ListNode cur = head;
while(cur != null){
ListNode flag = cur.next;
cur.next = pre;
pre = cur;
cur = flag;
}
return cur;
}
}