Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 55 additions & 0 deletions main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#include <stack>
#include <iostream>
using namespace std;

class ListNode {
public:
int val;
ListNode *next;
ListNode(int v) {
val=v;
next=NULL;
}
};

ListNode *AddNode() {
int val;
cout << "Data Element : " << endl;
cin >> val;
ListNode *newNode=new ListNode(val);
return newNode;
}

bool isPalindrome(ListNode *first) {
ListNode *head=first;
stack<int>st;
while(head) {
st.push(head->val);
head=head->next;
}
head=first;
while(head) {
if(st.top()!=head->val) {
return false;
}
head=head->next;
st.pop();
}
return true;
}

int main() {
int n;
cout << "No. of Nodes in Linked List : " << endl;
cin >> n;
ListNode *first=AddNode();
ListNode *previous=first;
for(int i=0; i<n-1; i=i+1) {
ListNode *newNode=AddNode();
previous->next=newNode;
previous=newNode;
}

cout << isPalindrome(first);
return 0;
}