C Program to Check whether linked list is palindrome or not

Check whether linked list is palindrome or not

C Program to Check whether linked list is palindrome or not in which we have find that last node element matches with first node element and second node element matches with second last node element in the linked list and so on ,then it called palindrome otherwise not Or we can say that a list which is same when read from the starting and when read from last.
For Example :-
Input :- 4 -> 51 -> 16 -> 30 -> 16 -> 51 -> 4
Output :- The list is palindrome.
Input :- 14 -> 5 -> 11 -> 30 -> 11 -> 51 -> 14
Output :- The list is not palindrome.

C Program to Check whether linked list is palindrome or not

Implementation for Checking a list is palindrome or not :-

  • First get the middle node of the given Linked List let take Consideration for both the odd and even cases.
  • Then, we will reverse the second half of the Linked List.
  • We will compare the second half with the first half if both the halves are identical then the linked list is a palindrome.
  • Reconstruct the actual given Linked List by again reversing the second half and attaching it back to the first half.
Linked-List-is-a-Palindrome-or-not

Structure of the node in the Singly Linked List :-

struct node
{
int data;
struct node *next;
};

C Program to Check whether linked list is palindrome or not:-

Run
#include<stdio.h> 
#include<stdlib.h> 

struct Node
{
  int data;
  struct Node *next;
};


void insertFirst (struct Node **head, int data)
{

  // dynamically create memory for this newNode
  struct Node *newNode = (struct Node *) malloc (sizeof (struct Node));

  // assign data value
  newNode->data = data;
  // change the next node of this newNode 
  // to current head of Linked List
  newNode->next = *head;

  //re-assign head to this newNode
  *head = newNode;
}

void display (struct Node *node)
{
  printf ("Linked List : \n");

  // as linked list will end when Node is Null
  while (node != NULL)
    {
      printf ("%d ", node->data);
      node = node->next;
    }
  printf ("\n");
}

int size (struct Node *node)
{
  int counter=0;

  // as linked list will end when Node is Null
  while (node != NULL)
    {
      node = node->next;
      counter++;
    }
 return counter;
    
}
int checkPalindrome (struct Node *head, int counter)

{
    int i = 0, j;
    struct Node *front, *rear;
     while (i != counter / 2)
    {
        front = rear = head;
        for (j = 0; j < i; j++)
        {
            front = front->next;
        }
        for (j = 0; j < counter - (i + 1); j++)
        {
            rear = rear->next;
        }
        if (front->data != rear->data)
        {
            return 0;
        }
        else
        {
            i++;
        }
    }

    return 1;
}

int main ()
{
  struct Node *head = NULL;
  int counter,result;
  // Need '&' i.e. address as we need to change head
  insertFirst (&head, 2);
  insertFirst (&head, 3);
  insertFirst (&head, 4);
  insertFirst (&head, 3);
  insertFirst (&head, 2);

  // no need of '&' as we are not changing head just displaying Linked List
  display (head);
  counter = size(head);
      result = checkPalindrome (head, counter);

    if (result == 1)
    {
        printf("The linked list is a palindrome.\n");
    }
    else
    {
        printf("The linked list is not a palindrome.\n");
    }
  return 0;
}
Linked List :
2 3 4 3 2 
The linked list is a palindrome.