jayati

Check if Linked List is Palindrome

Oct 11th, 2023
1,276
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.51 KB | None | 0 0
  1. //{ Driver Code Starts
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <iostream>
  5. #include <stack>
  6. using namespace std;
  7. /* Link list Node */
  8. struct Node {
  9.   int data;
  10.   struct Node *next;
  11.   Node(int x) {
  12.     data = x;
  13.     next = NULL;
  14.   }
  15. };
  16.  
  17.  
  18.  
  19.  
  20. // } Driver Code Ends
  21. /*
  22. struct Node {
  23.   int data;
  24.   struct Node *next;
  25.   Node(int x) {
  26.     data = x;
  27.     next = NULL;
  28.   }
  29. };
  30. */
  31.  
  32. class Solution{
  33.   public:
  34.     //Function to check whether the list is palindrome.
  35. Node* reverseLinkedList(Node* head) {
  36.     Node* prev = NULL;
  37.     Node* current = head;
  38.     Node* nextNode;
  39.  
  40.     while (current != NULL) {
  41.         nextNode = current->next;
  42.         current->next = prev;
  43.         prev = current;
  44.         current = nextNode;
  45.     }
  46.  
  47.     return prev;
  48. }
  49. // Function to copy a linked list
  50. Node* copyLinkedList(Node* head) {
  51.     if (head == NULL) return NULL;
  52.  
  53.     Node* nHead = new Node(head->data);
  54.     Node* current = nHead;
  55.     Node* original = head->next;
  56.  
  57.     while (original != NULL) {
  58.         current->next = new Node(original->data);
  59.         current = current->next;
  60.         original = original->next;
  61.     }
  62.  
  63.     return nHead;
  64. }
  65.  
  66.     bool isPalindrome(Node *head)
  67.     {
  68.         //Your code here
  69.          Node* nhead = copyLinkedList(head);
  70.          Node* newHead = reverseLinkedList(head);
  71.        
  72.          Node* temp=newHead;
  73.          
  74.          Node* temp2=nhead;
  75.          bool f=true;
  76.          while(temp!=NULL && temp2!=NULL)
  77.          {
  78.             //  cout<<temp->data<<" "<<temp2->data<<endl;
  79.              if(temp->data!=temp2->data)
  80.              {
  81.                  f=false;
  82.                  break;
  83.              }
  84.              temp=temp->next;
  85.              temp2=temp2->next;
  86.          }
  87.          if(f)
  88.          {
  89.              return 1;
  90.          }
  91.          else
  92.          {
  93.              return 0;
  94.          }
  95.        
  96.        
  97.     }
  98. };
  99.  
  100.  
  101.  
  102. //{ Driver Code Starts.
  103. /* Driver program to test above function*/
  104. int main()
  105. {
  106.   int T,i,n,l,firstdata;
  107.     cin>>T;
  108.     while(T--)
  109.     {
  110.        
  111.         struct Node *head = NULL,  *tail = NULL;
  112.         cin>>n;
  113.         // taking first data of LL
  114.         cin>>firstdata;
  115.         head = new Node(firstdata);
  116.         tail = head;
  117.         // taking remaining data of LL
  118.         for(i=1;i<n;i++)
  119.         {
  120.             cin>>l;
  121.             tail->next = new Node(l);
  122.             tail = tail->next;
  123.         }
  124.     Solution obj;
  125.     cout<<obj.isPalindrome(head)<<endl;
  126.     }
  127.     return 0;
  128. }
  129.  
  130.  
  131. // } Driver Code Ends
Advertisement
Add Comment
Please, Sign In to add comment