# C++ time: O(n) space:O(1) easy to understand with comments.

• first reverse the second half ans compare it with first half to check whether linked list is palindrome or not.

``````     struct ListNode * rev(struct ListNode * head)  //function for reverse the list
{
if(b) c=b->next;
while(b)
{
b->next=a;
a=b;
b=c;
if(c) c=c->next;
}
return a;
}

class Solution {
public:
struct ListNode *s,*f;
while(f->next && f->next->next)
{
s=s->next;
f=f->next->next;
}
s=s->next;
s=rev(s);   // s is reversed second half
while(s!=0)  //compare first half with second half
{
if(s->val==f->val){s=s->next,f=f->next;}
else return false;
}
return true;

}
};``````

• `````` bool isPalindrome(ListNode* head) {
stack<int> s;
int length=0;
while(move!=NULL)
{
s.push(move->val);
length++;
move=move->next;
};
if(length==0||length==1)
return true;
for(int i=0;i<length/2;i++)
{
if(s.top()!=move->val)
return false;
move=move->next;
s.pop();
};
return true;

}``````

• ``````class Solution {
public:
long forward = 0, backward = 0;
int i = 0;
while(curr){
forward = (forward << 1) + curr->val;
backward += (long)(1 << i++) * (long)curr->val;
curr = curr->next;
}
return forward == backward;

}
};``````

• This might not pass on a list that has many INT_MAX elements.

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.