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.

Check whether linked list is palindrome or not in C

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.
C Program to Check whether linked list is palindrome or not

Structure of the node in the Singly Linked List :-

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

To study more about Linked List click below :-

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

#include <stdio.h> 
#include <stdlib.h> 
struct node
{
  int num;
  struct node *next;
};
int create_List(struct node **);
int palindrome_check(struct node *, int);
void release_Node(struct node **);
int palindrome_check(struct node *palindrome, int counter)
{
  int i = 0, j;
  struct node *front, *rear;
  while (i != counter / 2)
    {
      front = rear = palindrome;
      for (j = 0; j < i; j++)
	{
	  front = front->next;
	}
      for (j = 0; j < counter - (i + 1); j++)
	{
	  rear = rear->next;
	}
      if (front->num != rear->num)
	{
	  return 0;
	}
      else
	{
	  i++;
	}
    }
  return 1;
}
int create_List(struct node **head)
{
  int n, ch, counter = 0;
  struct node *temp;
  do
    {
      printf ("Enter the data into the list :  ");
      scanf ("%d ", &n);
      counter++;
      temp = (struct node *) malloc (sizeof (struct node));
      temp->num = n;
      temp->next = *head;
      *head = temp;
      printf ("Do you want enter more elements in the list press [1/0] :  ");
      scanf ("%d ", &ch);
     }
  while (ch != 0);
  printf ("\n ");
  return counter;
}
void release_Node(struct node **head)
{
  struct node *temp = *head;
  while ((*head) != NULL)
    {
      (*head) = (*head)->next;
      free (temp);
      temp = *head;
    }
}
int main ()
{
  struct node *palindrome = NULL;
  int result, counter;
  counter = create_List(&palindrome);
  result = palindrome_check (palindrome, counter);
  if (result == 1)
    {
      printf ("The linked list is a palindrome.\n ");
    }
  else
    {
      printf ("The linked list is not a palindrome.\n ");
    }
  release_Node(&palindrome);
  return  0;
}
Output:-
Enter the data into the list : 51
Do you want enter more elements in the list press [1/0] : 1
Enter the data into the list : 12
Do you want enter more elements in the list press [1/0] : 1
Enter the data into the list : 1
Do you want enter more elements in the list press [1/0] : 1
Enter the data into the list : 12
Do you want enter more elements in the list press [1/0] : 1
Enter the data into the list : 51
Do you want enter more elements in the list press [1/0] : 0

The linked list is a palindrome.