# Recursive solution, is it correct way?

• ``````public ListNode reverseBetween(ListNode head, int m, int n) {
ListNode temp = head;
ListNode parent = head;
if (m > n)

for (int i = 1; i < m && temp != null; i++) {
parent = temp;
temp = temp.next;
}
//applied a workaround if reverse to done from start program was creating a circular list
if (m > 1)
parent.next = reverseHelper(temp, m, n);
if (m == 1)
head = reverseHelper(temp, m, n);

}

private ListNode reverseHelper(ListNode temp, int m, int n) {
if (temp == null)
return null;
if (m == n)
return temp;
if (m > n)
return temp;
ListNode startNode = reverseHelper(temp.next, m + 1, n);
ListNode temp2 = startNode;
// cannot just check for null as we may want to stop in between
while (temp2 != null && temp2.next != null && m + 1 < n) {
m++;
temp2 = temp2.next;
}
if (m < n && temp2 == null)
return temp;

temp.next = temp2.next;
temp2.next = temp;
// cannot null the value unless it is last

return startNode;

}``````

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.