Leetcode:234. 回文链表
解法:先把链表中的元素值放入arrayList中,再判断arrayList的值是否回文来判断是否为回文链表。留神:不要把元素值放入int[]类型的数组,因为须要计算链表长度能力确定要开拓多大的int空间,消耗性能。
class Solution { public boolean isPalindrome(ListNode head) { ArrayList<Integer> arr = new ArrayList<Integer>(); while(head != null){ arr.add(head.val); head = head.next; } int first = 0; int last = arr.size() -1; while(first <= last){ if(arr.get(first) != arr.get(last)) return false; first++; last--; } return true; }}