https://leetcode-cn.com/problems/palindrome-linked-list/description/
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public boolean isPalindrome(ListNode head) {
int num=1;
if(head==null)
{
return true;
}
ListNode temp=head;
if(head.next==null)
{
return true;
}
while(temp.next!=null)
{
num++;
temp=temp.next;
}
// System.out.println(num);
if(num%2!=0)
{
int arr[]=new int[num/2+1];
for(int i=0;i<num/2;i++)
{
arr[i]=head.val;
head=head.next;
}
head=head.next;
for(int j=num/2-1;j>=0;j--)
{
if(arr[j]!=head.val)
{
return false;
}
else
{
head=head.next;
}
}
return true;
}
else
{
int arr[]=new int[num/2];
for(int i=0;i<num/2;i++)
{
arr[i]=head.val;
head=head.next;
}
for(int j=num/2-1;j>=0;j--)
{
if(arr[j]!=head.val)
{
return false;
}
else
{
head=head.next;
}
}
return true;
}
}
}