A question about the Java code


  • 0
    K

    First I want to clarify that the codes below are just written to try another method. I know it is not even close to an ideal solution.

    public class Solution {
        public boolean isPalindrome(ListNode head) {
            List l = new ArrayList();
            ListNode n = head;
            while(n != null){
                l.add(n.val);
                n = n.next;
            }
            int len = l.size();
            for(int i = 0; i < len; i++){
                if(l.get(i) != l.get(len-1-i)){
                    System.out.println(i);
                    System.out.println(len-1-i);
                    System.out.println(l.get(i));
                    System.out.println(l.get(len-1-i));  
                    return false;
                } 
            }
            return true;
        }
    }
    

    Those four println lines are to test whether the if clause obtains the right values.
    The test result is as below.

    Your input

    [-16557,-8725,-16557]

    Your stdout

    0
    2
    -16557
    -16557

    Your answer

    false

    Expected answer

    true

    From the result above, it seems that the code thinks -16557 != -16557.
    I tested many other inputs and all gave the correct answer. So I guess it has something to do with the range of input value? I'm not sure.


Log in to reply
 

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