思路一:利用双向链表,每次移除头节点和尾节点,如果不相等返回false;
class Solution {
public boolean isPalindrome(ListNode l1) {
if(l1 == null){
return false;
}
LinkedList<ListNode> doubleList = new LinkedList<ListNode>();
while (l1 != null){
doubleList.add(l1);
l1 = l1.next;
}
while (doubleList.size() > 1){
ListNode first = doubleList.removeFirst();
ListNode last = doubleList.removeLast();
if (first.val != last.val){
return false;
}
}
return true;
}
}