What's wrong with my code


  • 0
    N

    I have NuLLpointerexception on line 36
    I have no idea why
    Could any one help me figure it out?

    Thanks

    /**
     * Definition for singly-linked list.
     * public class ListNode {
     *     int val;
     *     ListNode next;
     *     ListNode(int x) { val = x; }
     * }
     */
    public class Solution {
        public boolean isPalindrome(ListNode head) {
            if(head == null) {
                return true;
            }
            ListNode start=head;
            ListNode end = head;
            ListNode middle = head;
            ListNode pre = head;
            ListNode next =start.next;
            int i=0;
            while(end.next!=null && end.next.next!=null){
                end=end.next.next;
                middle=next;
                next=next.next;
                middle.next=pre;
                pre=pre.next;
                
                i++;
            }
            start=next;
          
            if(end.next==null){
                middle=middle.next;
            }
              
                while(middle!=null){
                 
                    if(middle.val != start.val){
                        
                        return false;
                    }
                    middle=middle.next;
                    start=start.next;
                }
            
            return true;
        }
    }

Log in to reply
 

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