class Solution {
public ListNode reverseBetween(ListNode head, int left, int right) {
ListNode prehead = new ListNode(0);
prehead.next = head;
ListNode node = head;
int num = 1;
ListNode tou = prehead;
ListNode h = head;
ListNode wei = prehead;
ListNode w = head;
for(int i=0; i<left-1; i++){
tou=tou.next;
}
h = tou.next;
for(int i=0; i<right; i++){
wei = wei.next;
}
w = wei.next;
wei.next = null;
h = reverse(h);
tou.next = h;
wei = prehead;
for(int i=0; i<right; i++){
wei = wei.next;
}
wei.next = w;
return prehead.next;
}
public ListNode reverse(ListNode head){
ListNode pre = null;
ListNode node = head;
while(node!=null){
ListNode tmp = node.next;
node.next = pre;
pre = node;
node = tmp;
}
return pre;
}
}