no idea why my solution can't pass 1 specific test case


  • 0
    C

    here is my code:

    /**

    • 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) {
      // special cases
      if (head == null || head.next == null){
      return true;
      }

       // find list length and create array
       ArrayList<Integer> values = new ArrayList<Integer>();
       int list_length = 0;
       while (head != null){
           list_length++;
           values.add(head.val);
           head = head.next;
       }
       
       // check palindrome
       int left = 0, right = list_length - 1;
       while (left <= right){
           if (values.get(left) != values.get(right)){
               return false;
           }
           left++;
           right--;
       }
       return true;
      

      }
      }

    This solution is very not space O(1), I just can't figure out why it can't pass one test case.


Log in to reply
 

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