From f52fa7e57408840d3ca030ad71c3803f4149de7f Mon Sep 17 00:00:00 2001 From: Swanand Joshi Date: Mon, 15 Dec 2025 14:08:19 +0530 Subject: [PATCH] A sample Linked List problem for ISSUE 585. The problem is to check whether a linked list is a palindrome --- main.cpp | 55 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 main.cpp diff --git a/main.cpp b/main.cpp new file mode 100644 index 00000000..1b7a48e9 --- /dev/null +++ b/main.cpp @@ -0,0 +1,55 @@ +#include +#include +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; + stackst; + 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; inext=newNode; + previous=newNode; + } + + cout << isPalindrome(first); + return 0; +} \ No newline at end of file