From c22165337e44d698578853dd613e182d8af005cb Mon Sep 17 00:00:00 2001 From: Rohit Bindal Date: Fri, 22 Oct 2021 18:28:26 +0530 Subject: [PATCH] Create palindromic linked list --- .../Linked List/palindromic linked list | 46 +++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 dsa-roadmaps/Beginners/Algorithms/Linked List/palindromic linked list diff --git a/dsa-roadmaps/Beginners/Algorithms/Linked List/palindromic linked list b/dsa-roadmaps/Beginners/Algorithms/Linked List/palindromic linked list new file mode 100644 index 0000000..8ae292f --- /dev/null +++ b/dsa-roadmaps/Beginners/Algorithms/Linked List/palindromic linked list @@ -0,0 +1,46 @@ +Node* reverseList(Node* head) +{ + Node* c = head; + Node* p = NULL; + Node* n; + + while(c!=NULL) + { + n = c->next; + c->next = p; + p = c; + c= n; + } + head = p; + return head; +} + +bool isPalindrome(Node *head) +{ + //Your code here + if(head==NULL || head->next==NULL) return true; + + Node* slow= head; + Node* fast = head; + + while(fast->next!=NULL and fast->next->next!=NULL) + { + slow = slow->next; + fast = fast->next->next; + } + + slow->next = reverseList(slow->next); + slow = slow->next; + Node* d = head; + while(slow!=NULL) + { + if(d->data != slow->data) + { + return false; + } + slow = slow->next; + d = d->next; + } + + return true; +}