题解 | #链表内指定区间反转#
链表内指定区间反转
http://www.nowcoder.com/practice/b58434e200a648c589ca2063f1faf58c
import java.util.*;
public class Solution {
ListNode preNode = null;
public ListNode reverseBetween (ListNode head, int m, int n) {
// write code here
return partReverse(head,m,n);
}
public ListNode partReverse (ListNode node,int m,int n){
if(m==1){
return ReverseN(node,n);
}
node.next = partReverse(node.next,--m,--n);
return node;
}
public ListNode ReverseN(ListNode node,int n){
if(n == 1){
preNode = node.next;
return node;
}
ListNode newNode = ReverseN(node.next,--n);
node.next.next = node;
node.next = preNode;
return newNode;
}
}