思路
由于链表我们无法就行o1的判断下标索引,又因为我们其实只需要判断当前节点的对应值是否相等,不需要判断整个节点,所以我们选择考虑将按顺序将链表中的值单独取出放入可01索引列表中,然后前后一一匹配即可
Accode:
/**
* Definition for singly-linked list.
public class ListNode {
int val;
ListNode next;
ListNode() {}
ListNode(int val) { this.val = val; }
ListNode(int val, ListNode next) { this.val = val; this.next = next; }
}
*/
class Solution {
public boolean isPalindrome(ListNode head) {
if(head==null||head.next==null)return true;
ArrayList<Integer> list = new ArrayList<>();
while(head!=null){
list.add(head.val);
head=head.next;
}
for (int i = 0; i <list.size()/2 ; i++) {
if(list.get(i)!=list.get(list.size()-1-i)){
return false;
}
}
return true;
}
}
over~