Palindrome Link List using an ArrayList | Accepted


  • 0
    U
    class Solution {
        public boolean isPalindrome(ListNode head) {
            if(head==null)
                return true;
            List arrayList=new ArrayList();
            
            while(head!=null){
                arrayList.add(head.val);
                head=head.next;
            }
          
            int length=arrayList.size();
            int i=0;
            int j=length-1;
            boolean isPalindrome=true;
            while(i<j){
              if(Integer.parseInt(arrayList.get(i).toString())!=Integer.parseInt(arrayList.get(j).toString()))
                  isPalindrome=false;
              i++;
              j--;
            }
            return isPalindrome;
        }
    }
    

Log in to reply
 

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