StackOver Flow error


  • 0
    A

    this code is giving stackoverflow error for one of the testcases ,the testcase was quite big though, can i get any help??

    public class Solution {
    
        static ListNode start=null;
        static boolean flag=true;
        public boolean isPalindrome(ListNode head) 
        {
    
            start=head;
            if(head==null)
                return true;
            
            else
            {
            boolean ans=solve(head);
            if(!flag)
                return false;
            else 
            return ans ;
            }
        }
        
        boolean solve(ListNode head)
        {
            if(head!=null)
            {
                solve(head.next);
                    System.out.println("--> "+start.val);
                if(start.val==head.val)
                {
    
                    start=start.next;
                    return true;
                }
                else
                    flag=false;
            }
            return true;
        }
    }
    
    

Log in to reply
 

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