I am writing a program to check whether a singly linked list is a palindrome or not. I am using the concept of reversing through iteration.
I have inserted 2,3,5,3,2 in the linked list and reversed it based on the thought that if the linked list obtained after reversing is the same as before reversing then it is a palindrome. But I am not able to conclude the program with the match statement. How can i match both the list ?
Here is my code:
struct Node{
int data;
Node* next;
};
Node*head;
void Insert(int x)
{
Node*temp=new Node();
temp->data=x;
temp->next=head;
head=temp;
}
void print()
{
Node*temp=head;
cout<<"list is";
while(temp!=NULL)
{
cout<<temp->data;
temp=temp->next;
}
}
void rec()
{
Node*current, *prev, *next;
current=head;
prev=NULL;
while(current!=NULL)
{
next= current->next;
current->next= prev;
prev= current;
current=next;
}
head=prev;
}
int main()
{
clrscr();
Node*head=NULL;
Insert(2);
Insert(3);
Insert(5);
Insert(3);
Insert(2);
cout<<"list before reversing is\n";
print();
cout<<"\n";
cout<<"list after reversing is \n";
rec();
print();
getch();
}