Check if Linked List is Palindrome | Practice | GeeksforGeeks

Given a singly linked list of size N of integers. The task is to check if the given linked list is palindrome or not.
Example 1:
Input:
N = 3
value[] = {1,2,1}
Output: 1
Explanation: The given linked list is
1 2 1 , which is a palindrome and
Hence, the output is 1.

Example 2:
Input:
N = 4
value[] = {1,2,3,4}
Output: 0
Explanation: The given linked list
is 1 2 3 4 , which is not a palindrome
and Hence, the output is 0.
Your Task:
The task is to complete the function isPalindrome() which takes head as reference as the only parameter and returns true or false if linked list is palindrome or not respectively.
Expected Time Complexity: O(N)
Expected Auxialliary Space Usage: O(1)  (ie, you should not use the recursive stack space as well)
Constraints:
1 <= N <= 105
+1
anupamyadav506123 hours ago
#include<vector>class Solution{   private:   bool checkPalindrome(vector<int>arr){       int n = arr.size();       int s=0;      // int e=0;       int e = n-1;                     while(s<=e){           if(arr[s] != arr[e]){               return 0;           }           s++;           e--;       }       return 1;   } public:   //Function to check whether the list is palindrome.   bool isPalindrome(Node *head)   {      vector<int> arr;         Node* temp = head;   while(temp != NULL){       arr.push_back(temp -> data);       temp = temp -> next;   }   return checkPalindrome(arr);                 }};
 
0
2019sushilkumarkori2 days ago
public:
    //Function to check whether the list is palindrome.
    Node* reverse(Node *head){
        Node* curr = head;
        Node* prev = NULL;
        Node* nextptr;
        while(curr!=NULL){
            nextptr = curr->next;
            curr->next = prev;
            prev = curr;
            curr = nextptr;
        }
        return prev;
    }
    bool isPalindrome(Node *head)
    {
        //Your code here
        Node* curr = head;
        int n = 0;
        while(curr!=NULL){
            n++;
            curr=curr->next;
        }
        curr = head;
        int i=0;
        if(n%2 == 0){
            while(i<=(n/2)){
                i++;
                if(i==(n/2)){
                    break;
                }
                curr = curr->next;
            }
            Node * t = curr->next;
            t = reverse(t);
            curr = head;
            int count=0;
            while(t!=NULL){
                if(t->data == curr->data){
                    count++;
                }
                t=t->next;
                curr = curr->next;
            }
            if(count == n/2){
                return true;
            }
            else{
                return false;
            }
        }
        else{
             while(i<=(n/2)+1){
                i++;
                if(i==(n/2)+1){
                    break;
                }
                curr = curr->next;
            }
            Node * t = curr->next;
            t = reverse(t);
            curr = head;
            int count=0;
            while(t!=NULL){
                if(t->data == curr->data){
                    count++;
                }
                t=t->next;
                curr = curr->next;
            }
            if(count == n/2){
                return true;
            }
            else{
                return false;
            }
        }
    }
};
0
fauzansiddiqui3 days ago
    private:
     int length(Node* head){
        Node* temp = head;
        int l = 0;
        while(temp != NULL)
        {
            l++;
            temp = temp->next;
        }
        return l;
    }
    Node* reverse(Node* head){
        Node* prevptr = NULL;
        Node* currptr = head;
        Node* nextptr;
        while(currptr != NULL){
            nextptr = currptr->next;
            currptr->next = prevptr;
            
            prevptr = currptr;
            currptr = nextptr;
            
        }
        return prevptr;
    }
   
  public:
    //Function to check whether the list is palindrome.
    bool isPalindrome(Node *head)
    {
        //Your code here
        int mid = length(head)/2 -1;
        Node* temp = head;
        while(temp!=NULL && mid>0){
            mid--;
            temp = temp->next;
        }
        Node* reversedHead = reverse(temp);
        int ld = length(head);
        temp = head;
        while(temp != NULL && reversedHead != NULL && ld >0){
            if(temp->data != reversedHead->data){
                 return false; 
            }
            temp = temp->next;
            reversedHead = reversedHead->next;
            ld--;
        }
        if(ld == 0)
        return true;
        return false;
    }
0
fauzansiddiqui
This comment was deleted.
0
lewiveb4346 days ago
class Solution{ public:   //Function to check whether the list is palindrome.      bool isPalin(Node** left, Node* right){       if(right==NULL) return true;       bool res=isPalin(left,right->next);       if(right->data!=(*left)->data) return res=res && false;       *left=(*left)->next;       return res;   }   bool isPalindrome(Node *head)   {       //Your code here       return isPalin(&head,head);   }};
0
annanyamathur1 week ago
  Node *fidMid(Node *head)
    {
        Node*slow=head,*fast=head->next;
        while(fast &&fast->next)
        {
            slow=slow->next;
            fast=fast->next->next;
        }
        return slow;
    }
    Node *reverse(Node *head)
    {
        Node *prev=NULL,*curr=head;
        while(curr)
        {
            Node*temp=curr;
            curr=curr->next;
            temp->next=prev;
            prev=temp;
        }
        return prev;
    }
    bool isPalindrome(Node *head)
    {
        if(!head) return false;
        if(!head->next) return true;
        Node *mid=fidMid(head);
        //cout<<mid->data<<endl;
        Node *temp=reverse(mid->next);
        mid->next=NULL;
        while(temp && head)
        {
            if(temp->data!=head->data)
            return false;
            temp=temp->next;
            head=head->next;
        }
        return true;
    }
+1
prateek_jakhar1 week ago
JAVA RECURSIVE
class Solution
{
    //Function to check whether the list is palindrome.
    Node left;
    boolean isPalindrome(Node head) 
    {
        left = head;
        boolean result = isPalindromeUtil(head);
        return result;
    }    
    boolean isPalindromeUtil(Node right){
        if (right == null)
            return true;
        
        boolean isp = isPalindromeUtil(right.next);
        if (isp == false)
            return false;
            
        boolean isp1 = (right.data == left.data);
 
        left = left.next;
        return isp1;
    }
}
0
aakashsoni24862 weeks ago
O(n) and space is also O(n)
class Solution{ public:   //Function to check whether the list is palindrome.   bool isPalindrome(Node *head)   {       //Your code here       string s="";       Node*cur=head;       while(cur!=NULL){           s+=cur->data-'0';           cur=cur->next;       }       int i=0;       int j=s.size()-1;       while(i<j){           if(s[i]!=s[j]) return 0;           i++;           j--;       }       return 1;   }};
+1
wolfofsv2 weeks ago
O(N) time and O(1) space
find length
reverse the second half of the LL
compare with two pointers, one at the beginning and another at the middle
class Solution{
  public:
    //Function to check whether the list is palindrome.
    int FindLength(Node * head){
        int len = 0;
        while(head != NULL){
            head = head -> next;
            len++;
        }
        return len;
    }
    
    Node * ReverseUtil(Node * head){
        Node * prev = NULL, *curr = head;
        
        
        while(curr != NULL){
            Node* store = curr -> next;
            curr -> next = prev;
            prev = curr;
            curr = store;
        }
        
        return prev;
    }
    
    Node* ReverseLL(Node * head, int start){
        Node* prev = NULL, *curr = head;
        for(int i = 0; i < start; i++){
            prev = curr;
            curr = curr -> next;
        }
        prev -> next = ReverseUtil(curr);
        return prev -> next;
    }
    
    bool isPalindrome(Node *head)
    {
        //Your code here
        if(head -> next == NULL){
            return true;
        }
        
        int len = FindLength(head);
        Node* head2 = ReverseLL(head, (len+1)/2);
        
        
        Node* head1 = head;
        while(head2 != NULL){
            if(head2 -> data != head1 -> data){
                return false;
            }
            head1 = head1 -> next;
            head2 = head2 -> next;
        }
        return true;
    }
};


0
harshscode2 weeks ago
easy solution...
 Node *cur=head;       string s="",rev="";              while(cur)       {           s+=cur->data;           cur=cur->next;       }       for(int i=s.length()-1;i>=0;i--)       {           rev+=s[i];       }              for(int i=0;i<s.length();i++)       {           if(s[i]==rev[i])           continue;           else           return false;       }       return true;
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.