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.

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.

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

//re-assign head to this 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

// no need of '&' as we are not changing head just displaying Linked List
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.
```

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

• Introduction to Linked List in Data Structure
• Linked List in – C | C++ | Java
• Singly Linked List in – C | C++ | Java
• Insertion in singly Linked List – C | C++ | Java
• Insertion at beginning in singly Linked List  – C | C++Java
• Insertion at nth position in singly Linked List  – C | C++Java
• Insertion at end in singly Linked List  – C | C++Java
• Deletion in singly Linked List  – C | C++Java
• Deletion from beginning in singly linked list : C | C++ | Java
• Deletion from nth position in singly linked list : C | C++ | Java
• Deletion from end in singly linked list : C | C++ | Java
• Reverse a linked list without changing links between nodes (Data reverse only) – C | C++Java
• Linked List Insertion and Deletion – C | C++Java
• Reverse a linked list by changing links between nodes – C | C++Java
• Linked List insertion in the middle – C | C++Java
• Print reverse of a linked list without actually reversing – C |C++ | Java
• Search an element in a linked list – C | C++Java
• Insertion in a Sorted Linked List – C | C++Java
• Delete alternate nodes of a Linked List – C | C++Java
• Find middle of the linked list – C | C++Java
• Reverse a linked list in groups of given size – C | C++Java
• Find kth node from end of the linked list – C | C++Java
• Append the last n nodes of a linked list to the beginning of the list – C | C++Java
• Check whether linked list is palindrome or not – C | C++Java
• Fold a Linked List – C | C++Java
• Insert at a given position – C | C++Java
• Delete at a given position – C | C++Java

• Introduction to Linked List in Data Structure
• Linked List in –
C | C++ | Java
• Singly Linked List in –
C | C++ | Java
• Insertion in singly Linked List –
C | C++ | Java
• Insertion at beginning in singly Linked List  –
C | C++Java
• Insertion at nth position in singly Linked List  –
C | C++Java
• Insertion at end in singly Linked List  –
C | C++Java
• Deletion in singly Linked List  –
C | C++Java
• Deletion from beginning in singly linked list :
C | C++ | Java
• Deletion from nth position in singly linked list :
C | C++ | Java
• Deletion from end in singly linked list :
C | C++ | Java
• Linked List Insertion and Deletion –
C | C++Java
• Reverse a linked list without changing links between nodes (Data reverse only) –
C | C++Java
• Reverse a linked list by changing links between nodes –
C | C++Java
• Print reverse of a linked list without actually reversing –
C |C++Java
• Print reverse of a linked list without actually reversing –
C |C++Java
• Insertion in the middle Singly Linked List –
C | C++Java
• Insertion in a Sorted Linked List –
C | C++Java
• Delete alternate nodes of a Linked List –
C | C++Java
• Find middle of the linked list –
C | C++Java
• Reverse a linked list in groups of given size –
C | C++Java
• Find kth node from end of the linked list –
C | C++Java
• Append the last n nodes of a linked list to the beginning of the list –
C | C++Java
• Check whether linked list is palindrome or not –
C | C++Java
• Fold a Linked List –
C | C++Java
• Insert at given Position –
C | C++Java
• Deletion at given Position –
C | C++Java