Check palindrome LL
You have been given a head to a singly linked list of integers. Write a function check to whether the list given is a 'Palindrome' or not.
Input format :
Remember/Consider :
Output format :
Constraints :
Sample Input 1 :
Sample Output 1 :
Sample Input 2 :
Sample Output 2 :
Explanation for the Sample Input 2 :
/****************************************************************
Following is the class structure of the Node class:
class Node
{
public:
int data;
Node *next;
Node(int data)
{
this->data = data;
this->next = NULL;
}
};
*****************************************************************/
Node* printReverse(Node *head)
{
//Write your code here
Node* prv = NULL;
Node* cur = head;
Node* nxt;
while(cur!=NULL){
nxt=cur->next;
cur->next=prv;
prv=cur;
cur=nxt;
}
return prv;
}
bool isPalindrome(Node *head)
{
//Write your code here
if (head==NULL){
return true;
}
Node* head_rev=printReverse(head);
Node* temp1=head;
Node* temp2=head_rev;
while(temp1->next!=NULL){
if(temp1->data!=temp2->data){
return false;
}
temp1=temp1->next;
temp2=temp2->next;
}
return true;
}
Comments
Post a Comment