From ad092aab6c4b61f8db479cfbca482e9663e2bcf8 Mon Sep 17 00:00:00 2001 From: Pankajbhatt <76560049+pankajbhatt150@users.noreply.github.com> Date: Thu, 6 Oct 2022 20:05:20 +0530 Subject: [PATCH] Create Palindrome LinkedList.cpp --- Palindrome LinkedList.cpp | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 Palindrome LinkedList.cpp diff --git a/Palindrome LinkedList.cpp b/Palindrome LinkedList.cpp new file mode 100644 index 0000000..30d4149 --- /dev/null +++ b/Palindrome LinkedList.cpp @@ -0,0 +1,28 @@ +#include + +bool isPalindrome(Node *head) +{ + stack visitedNodes; + // push all nodes in the stack + Node *cur = head; + while (cur != NULL) + { + visitedNodes.push(cur); + cur = cur->next; + } + + cur = head; + while (cur != NULL) + { + // compare node values + Node *temp = visitedNodes.top(); + if (cur->data != temp->data) + { + return false; + } + visitedNodes.pop(); + cur = cur->next; + } + + return true; +}