题目内容:
解题代码:
public class PalindromeList {
public boolean chkPalindrome(ListNode A) {
ListNode cur = A;
if(cur == null){
return true;
}
int count = 0;
while(cur != null){
count++;
cur = cur.next;
}
cur = A;
int[] arr = new int[count];
for(int k = 0;k < count;k++){
arr[k] = cur.val;
cur = cur.next;
}
int i = 0;
int j = count - 1;
while(i < j){
if(arr[i] == arr[j]){
i++;
j--;
}else{
return false;
}
}
return true;
}
}
解题思路:
这道题将借用数组完成,将链表结点的value赋值给数组,在借用对撞指针,从最左和最右开始遍历,直到left >= right 时循环,或者两个不相等时退出循环,前者返回true;后者返回false