Java Solution -> TLE not sure why


  • 0
    M
    public class Solution {
        public boolean isPalindrome(ListNode head) {
        boolean DEBUG_FLAG = false;
         ListNode Temp = head;
         ListNode middle = getMiddle(Temp);
         ListNode reverseList = reverse(middle);
            ListNode firstHalf = head;
            while(reverseList!=null){
                 if(reverseList.val==firstHalf.val){
                     reverseList = reverseList.next;
                      firstHalf = firstHalf.next;
                      DEBUG_FLAG = true;    
                     
                 }
                 else{
                     DEBUG_FLAG = false;
                 }
             }
             
            return DEBUG_FLAG;
        }
        
        public ListNode reverse(ListNode head){
            if(head==null || head.next==null){
                return head;
            }   
                ListNode reverse = reverse(head.next);
                head.next.next = head;
                head.next = null;
                 return reverse;
            
        }
        
        public ListNode getMiddle(ListNode head){
           if(head==null || head.next==null){
                return head;
            } 
           ListNode slow=head;      
           ListNode fast=head;
             while(fast.next!=null && fast.next.next!=null){
                 slow = slow.next;
                 fast =  fast.next.next;
            } 
            
            return slow; 
        }
    }

Log in to reply
 

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