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;
}
}
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。