Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //{ Driver Code Starts
- #include <stdio.h>
- #include <stdlib.h>
- #include <iostream>
- #include <stack>
- using namespace std;
- /* Link list Node */
- struct Node {
- int data;
- struct Node *next;
- Node(int x) {
- data = x;
- next = NULL;
- }
- };
- // } Driver Code Ends
- /*
- struct Node {
- int data;
- struct Node *next;
- Node(int x) {
- data = x;
- next = NULL;
- }
- };
- */
- class Solution{
- public:
- //Function to check whether the list is palindrome.
- bool isPalindrome(Node *head)
- {
- Node* fast=head;
- Node* slow=head;
- //finding mid point
- while(fast and fast->next){
- slow=slow->next;
- fast=fast->next->next;
- }
- //checking odd
- if(fast!=NULL){
- slow=slow->next;
- }
- //reversing 2nd half
- Node* curr=slow;
- Node* prev=NULL;
- Node* next=NULL;
- while(curr){
- next=curr->next;
- curr->next=prev;
- prev=curr;
- curr=next;
- }
- slow=prev;//head of reversed 2nd half
- while(slow and head){
- if(slow->data != head->data) return false;
- slow=slow->next;
- head=head->next;
- }
- return true;
- }
- };
- //{ Driver Code Starts.
- /* Driver program to test above function*/
- int main()
- {
- int T,i,n,l,firstdata;
- cin>>T;
- while(T--)
- {
- struct Node *head = NULL, *tail = NULL;
- cin>>n;
- // taking first data of LL
- cin>>firstdata;
- head = new Node(firstdata);
- tail = head;
- // taking remaining data of LL
- for(i=1;i<n;i++)
- {
- cin>>l;
- tail->next = new Node(l);
- tail = tail->next;
- }
- Solution obj;
- cout<<obj.isPalindrome(head)<<endl;
- }
- return 0;
- }
- // } Driver Code Ends
Advertisement
Add Comment
Please, Sign In to add comment