Given a singly linked list, determine if it is a palindrome.
Example 1:
Input: 1->2 Output: false
Example 2:
Input: 1->2->2->1 Output: true
Follow up:
Could you do it in O(n) time and O(1) space?
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
bool isPalindrome(struct ListNode* head) {
if(head==NULL||head->next==NULL)
return true;
struct ListNode *p=head;
int a[100000],i=0,j=0,k;
while(p->next!=NULL)
{
a[i++]=p->val;
p=p->next;
}
a[i]=p->val;
while(j<i)
{
if(a[i]!=a[j])
return false;
i--;
j++;
}
return true;
}