I had written the correct code.
Please tell where is mistake and correct the code and also add comments where correction is done.
Palindrome Linked List
Amardeep, you need to modify your approach a little bit as,
node* midPoint(node head)
{
nodeslow=head;
node*fast=head->next;
while(fast!=NULL && fast->next!=NULL)
{
fast=fast->next->next;
slow=slow->next;
}
return slow;
}
node* reverse(node head)
{
if(head->next==NULL)
{
return head;
}
nodesmallHead=reverse(head->next);
node*c=smallHead;
while(c->next!=NULL)
{
c=c->next;
}
c->next=head;
head->next=NULL;
return smallHead;
}
bool palindrome(node *head,int n)
{
int flag=1;
int count=n;
node*temp=midPoint(head); // middle
temp=reverse(temp);
int step=0;
while(step<(count/2))
{
step++;
if(head->data!=temp->data)
{
flag=0;
}
else
{
head=head->next;
temp=temp->next;
}
}
return flag;
}
Firstly, you will determine the middle point in linked list… and then use a recursive function to reverse the linked list from middle and then check if the reverse head and original linked list heads are same or different and perform the operation accordingly…