Same solution Accepted on Leetcode, runtime error on Interview Bit


#1

Now i have started hating this platform
whenever i use recursion in Linked list type of problems its giving me stack overflow error, The same solution gave me 0 ms runtime and faster than 100% java submissions on leetcode, but here its giving me runtime error
Please Please increase the heap size of your java runtime, there are people who are fan of recursion
my solution
private ListNode rotatedListNodePtr = null;
private int counterVal = 0;
private int pivotPoint = -1;
public ListNode rotateRight(ListNode A, int B) {
pivotPoint = B;
if(A == null)
return null;
ListNode ptr = rotateList(A, 0);
if(ptr == null)
return rotatedListNodePtr;

if(rotatedListNodePtr == null)
return ptr;

ListNode start = rotatedListNodePtr;
while (start.next != null)
start = start.next;

start.next = ptr;
return rotatedListNodePtr;
}

private ListNode rotateList(ListNode node, int len){
if(node == null){
if(pivotPoint > len)
pivotPoint = pivotPoint%len;
return null;
}

node.next = rotateList(node.next, len+1);
counterVal++;
if(counterVal == pivotPoint){
rotatedListNodePtr = node;
return null;
}
return node;
}