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; +}