fork download
  1. #include <iostream>
  2. #include<stack>
  3. using namespace std;
  4. struct node{
  5. int data;
  6. struct node* next;
  7. };
  8. struct node* createnode(int val)
  9. {
  10. struct node* p=(struct node*)malloc(sizeof(struct node));
  11. p->data=val;
  12. p->next=NULL;
  13. return p;
  14. }
  15. int palindrome(struct node* head)
  16. {
  17. struct node* cur=head;
  18. if(head==NULL)
  19. return 0 ;
  20. stack<int> s;
  21. while(cur!=NULL)
  22. {
  23. s.push(cur->data);
  24. cur=cur->next;
  25. }
  26. while(head)
  27. {
  28. int c=s.top();
  29. s.pop();
  30. if(head->data==c)
  31. {
  32. head=head->next;
  33. }
  34. else
  35. {
  36. return 0;
  37. }
  38. }
  39. return 1;
  40. }
  41.  
  42.  
  43. int main() {
  44. struct node* head=createnode(1);
  45. head->next=createnode(1);
  46. head->next->next=createnode(2);
  47. head->next->next->next=createnode(1);
  48. int res=palindrome(head);
  49. //cout<<isalpha;
  50. cout<<res;
  51. return 0;
  52. }
Success #stdin #stdout 0.01s 5284KB
stdin
Standard input is empty
stdout
Standard output is empty