even after passing all test cases why the hackerblocks compiler showing segmentation fault in palindrome question
#include
using namespace std;
class node
{
public:
int data;
node *next;
node *pre=NULL;
node(int d)
{
data = d;
next = NULL;
}
};
void end(node *&head, int d)
{
if (head == NULL)
{
head = new node(d);
return;
}
node *tail = head;
while (tail->next != NULL)
{
tail = tail->next;
}
node *temp = new node(d);
tail->next = temp;
temp->pre = tail;
return;
}
void print(node head)
{
while (head != NULL)
{
cout << head->data <<" ";
head = head->next;
}
}
void printend(node head){
node tail = head;
while (tail->next != NULL)
{
tail = tail->next;
}
cout<<endl;
while(tail!=NULL){
cout<data<<" ";
tail=tail->pre;
}
}
node input(node* &head,int n){
int d;
while(n–){
cin>>d;
end(head,d);
}
return head;
}
bool palindrome(node* head){
node* temp=head;
node* start=head;
while(temp->next!=NULL){
temp=temp->next;
}
while(start!=NULL || temp!=NULL){
if(temp->data!=start->data){
return false;
}
temp=temp->pre;
start=start->next;
}
return true;
}
int main()
{
int k;
cin>>k;
node *head = NULL;
head=input(head,k);
// print(head);
bool flag=palindrome(head);
if(flag==0){
cout<<“false”;
}
else{
cout<<“true”;
}
return 0;
}