1#include<bits/stdc++.h>
2
3using namespace std;
4
5// Declaration of a single Node
6class Node {
7public:
8 int data;
9 Node(int d){
10 data = d;
11 }
12 Node *ptr;
13};
14
15// Function that returns boolean value
16bool isPalin(Node* head){
17
18 // Temp pointer
19 Node* slow= head;
20
21 // Create a stack
22 stack <int> s;
23
24
25 // First traversal to push all the elements to stack
26 while(slow != NULL){
27 s.push(slow->data);
28 slow = slow->ptr;
29 }
30
31 // Second Traversal to compare the stack and node
32 while(head != NULL ){
33
34 int i=s.top();
35 s.pop();
36
37 // Compare data
38 if(head -> data != i){
39 return false;
40 }
41 head=head->ptr;
42 }
43
44return true;
45}
46
47// Driver Function
48int main(){
49 // Create nodes
50 Node one = Node(31);
51 Node two = Node(32);
52 Node three = Node(33);
53 Node four = Node(34);
54 Node five = Node(35);
55
56 // Connect all the nodes
57 five.ptr = NULL;
58 one.ptr = &two;
59 two.ptr = &three;
60 three.ptr = &four;
61 four.ptr = &five;
62 Node* temp = &one;
63
64
65 // Call function to return bool if the list is palindrome or not
66 int result = isPalin(&one);
67
68 if(result == 1)
69 cout<<"The value is True\n";
70 else
71 cout<<"The value is False\n";
72
73return 0;
74}