Check If Linked List Is Palindrome

Problem Statement

We will be given a linked list, and we need to find whether the given list is palindrome or not.

Problem Statement Understanding

To understand this problem statement, let us take examples.

If the given linked list is 3→1→18→1→3→NULL, and now according to the problem statement, we need to determine whether the given linked list is palindrome or not.

  • If we iterate the given list forward and print the elements, we will get: 3,1,18,1,3
  • Now, if we iterate the given list backward and print the elements, we will get: 3,1,18,1,3.
  • We can see that no matter we iterate forward or backward, we are getting the same output at the end. So, the given list is a palindrome.

At this point, we have understood the problem statement. Now we will try to formulate an approach for this problem.

Before moving to the approach section, try to think about how you can approach this problem.

  • If stuck, no problem, we will thoroughly see how we can approach this problem in the next section.

Let’s move to the approach section.

Approach 1

To check whether a linked list is a palindrome or not, we need to traverse the list backward from the last node of the list, but as our linked list is a singly linked list, we cannot move backwards in the list.

To solve the above problem, we will use a stack and store all the elements of the list in the stack while forward iteration of the list.

  • After that, we will again iterate the list and will simultaneously pop an element from the stack at each iteration and will check if this popped element is equal to the current element of the list or not.
  • If all the elements are equal, then the list is a palindrome. Else, It is not a palindrome.

Time Complexity: O(n), where n is the total number of nodes in the list
Space Complexity: O(n), due to the stack

The above approach seems fine, but we are using a stack that is increasing the space complexity. So, we need to think of a better approach where no extra space is used.

Approach 2

Since we cannot move backward in the list, we need to devise an algorithm to get our work done.

We can reverse the second half of the list and then simultaneously compare the elements from the first and second half of the list.

  • If all elements of first and the second half are equal, then the list is a palindrome.
  • Else, the list is not a palindrome.

Algorithm

1) If the list is empty or it has only one node then simply return true as an empty list or a list with a single node is always a palindrome.
2) Initialize two pointers slow and fast with the first node of the list (finding the middle node of the linked list).

  • Run a while loop till the fast reach the last or last second node of the list.
  • In each iteration, advance slow forward by one node and fast forward by two nodes.
  • Now, when the fast is at the end of the list, slow will be pointing to the middle node of the linked list.
    3) Now, reverse the second half of the linked list starting from slow->next to the last node of the linked list.
    4) After that, simultaneously iterate through the first half and the second half of the linked list using pointers head and slow:
  • If the data of the node to which head and slow are pointing are not equal, return false.
  • Else, advance slow and head by one node.
    5) After execution of the above while loop, return true from the function.

Dry Run


Code Implementation

#include
using namespace std;

class Node{
    public:
    int data;
    Node* next;
    Node(int x){
        data = x;
        next = NULL;
    }
};

// This function will reverse the list
Node* reverse(Node *head)
{
    Node *prev = NULL, *current = head, *next;
    while (current != NULL)
    {
        next  = current->next;
        current->next = prev;
        prev = current;
        current = next;
    }
    return prev;
}

// This function checks if a list is palindrome 
bool isPalindrome(Node* head) {
    //check if the list is empty or 
    //it has a single node
    if(head==NULL||head->next==NULL)
        return true;

    //initialize two pointers 
    Node* slow=head;
    Node* fast=head;

    // find the middle node of the list
    while(fast->next!=NULL&&fast->next->next!=NULL){
        slow=slow->next;
        fast=fast->next->next;
    }

    // reverse the second half of the list
    slow->next=reverse(slow->next);

    //update the 'slow' pointer
    slow=slow->next;

    //iterate both the lists simultaneously
    while(slow!=NULL){
        if(head->data!=slow->data)
            return false;
        head=head->next;
        slow=slow->next;
    }
    return true;
}


int main(void){
    Node* head = NULL;
    head = new Node(3);
    head->next = new Node(1);
    head->next->next = new Node(18);
    head->next->next->next = new Node(1);
    head->next->next->next->next = new Node(3);
    
    if(isPalindrome(head)){
        cout<<"The list is a palindrome";
    }else{
        cout<<"The list is not a palindrome";
    }
    return 0;
}
.

Output

The list is a palindrome

Time Complexity: O(n), n is the total number of nodes in the list.

So, in this blog, we have tried to explain how you can Check If a Linked List Is Palindrome most optimally. If you want to solve more questions on Linked List, which are curated by our expert mentors at PrepBytes, you can follow this link Linked List.

Previous post Deleting A Node In A Linked List In C
Next post Find Pairs With Given Sum In A Doubly Linked List

Leave a Reply

Your email address will not be published. Required fields are marked *