Whats wrong with this code?


  • 0
    R
    public class Solution {
        
         public ListNode reverseList(ListNode head) {
            if( head == null){
                return null;
            }
            
            if( head.next == null)
            {
                return head;
            }
            
             ListNode temphead = head.next;
             head.next = null;
             ListNode temp1;
             
             while(temphead != null)
             {
                 temp1 = temphead;
                 temphead = temphead.next;
                 temp1.next = head;
                 head = temp1;
                 
             }
            
            return head;
        }
        
        public boolean isPalindrome(ListNode head) {
            ListNode revhead = reverseList(head);
            
            while(head != null && revhead != null)
            {
                if(revhead.val == head.val){
                    revhead = revhead.next;
                    head = head.next;
                } else {
                    return false;
                }
            }
            
            return true;
        }
    }
    

Log in to reply
 

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