I get a stack overflow exception for one test case


  • 0
    V
    public class Solution {
    public boolean isPalindrome(ListNode head) {
        
        if(head == null || head.next == null)
          return true;
        
        ListNode[] l = new ListNode[1];
        boolean[] res = new boolean[1];
         boolean[] isDone = new boolean[1];
        res[0] = true;
        l[0] = head;
        isPalindromeAux(head,l,res,isDone);
        return res[0];
    }
    
    private void isPalindromeAux(ListNode current,ListNode[] l,boolean[] res,boolean[] isDone)
    {
        if(current == null)
          return;
        
        isPalindromeAux(current.next,l,res,isDone);
        
        if(isDone[0])
          return;
        
        if(l[0] == current || ((l[0].next == current) && (l[0].next.val == current.val)))
        {
            isDone[0] = true;
            return;
        }
        
        res[0] = res[0] && (current.val == l[0].val);
        l[0] = l[0].next;
    }
    

    }

    I get this runtime error (stack overflow) for a one test case with a huge input. I know there are better solutions out there by reversing the list and stuff but I am trying to understand why this one doesn't work.
    Looking forward to suggestions.


Log in to reply
 

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