# C++ program to check whether linked list is palindrome or not

## How to check if the given singly linked list is palindrome or not in C++?

A given list is said to be palindrome if it is same as original when reversed. For example, 1331, 178871, are palindrome. Here, in this article, we will be discussing how we can write a C++ program to check whether a linked list is palindrome or not along with its steps and algorithm.

To do this we will reach the end of the linked list using recursion and check if the data in the first node is equal to data in the last node and so on.

## Steps to write a C++ program to check whether a linked list is palindrome or not

#### Following steps are to be followed to check if the given linked list is palindrome or not.

1. Initialize the variables and declare the functions.
2. One to create linked list.
3. Second to display linked list.
4. Now make a recursive function to compare first and last node, second and second last node and so on.
5. Now check() function is used to check if linked list is palindrome or not.
6. Print result after comparing.

### Syntax

`class Node  {      int data;      Node *next;  }; `

## Algorithm to check if a linked list is palindrome or not

#### Following algorithm is used to check if the given linked list is palindrome or not

CHECKPALINDROME(STRUCT NODE **LEFT, STRUCT NODE* RIGHT)

1. IF RIGHT==NULL
2. RETURN 1
3. RES= CHECKPALINDROME(LEFT,RIGHT->NECXTPTR)&&((*LEFT)->NUM==RIGHT->NUM)
4. (*LEFT)=(*LEFT)->NEXTPTR
5. RETURN RES

CHECK(STRUCT NODE* STNODE)

• RETURN CHECKPALINDROME(&STNODE,STNODE)
• EXIT

## Program to check if a linked list is palindrome or not C++

Run
```#include<iostream>
using namespace std;

struct node
{
int num;
node *nextptr;
} *stnode;			//node constructed

void make (int n);
int checkPalindrome (struct node **left, struct node *right);
int check (struct node *stnode);
void display ();

int main ()
{
int n, num;

cout << "Enter the number of nodes: ";
cin >> n;
make (n);
cout << "\nLinked list data: \n";
display ();
if (check (stnode))
cout << "\nLinked list is Palindrome";
else
cout << "\nLinked list is not Palindrome";

return 0;
}

void make (int n)			//function to create linked list.
{
struct node *frntNode, *tmp;
int num, i;

stnode = (struct node *) malloc (sizeof (struct node));
if (stnode == NULL)
{
cout << "Memory can not be allocated";
}
else
{

cout << "Enter the data for node 1: ";
cin >> num;
stnode->num = num;
tmp = stnode;

for (i = 2; i <= n; i++)
{
frntNode = (struct node *) malloc (sizeof (struct node));

if (frntNode == NULL)	//memory cannot be allotted if frntnode is NULL
{
cout << "Memory can not be allocated";
break;
}
else
{
cout << "Enter the data for node " << i << ": ";	// Entering data in nodes.
cin >> num;
frntNode->num = num;
frntNode->nextptr = NULL;
tmp->nextptr = frntNode;
tmp = tmp->nextptr;
}
}
}
}

void display ()			//function to diplay linked list
{
struct node *tmp;
if (stnode == NULL)
{
cout << "List is empty";
}
else
{
tmp = stnode;
while (tmp != NULL)
{
cout << tmp->num << "\t";
tmp = tmp->nextptr;
}
}
}

int checkPalindrome (struct node **left, struct node *right)	// Function to check if linked list is palindrome or not
{
if (right == NULL)
return 1;

int res = checkPalindrome (left, right->nextptr)
&& ((*left)->num == right->num);
(*left) = (*left)->nextptr;

return res;
}

int check (struct node *stnode)	// Function to check if linked list is palindrome or not recursively
{
return checkPalindrome (&stnode, stnode);
}
```
```Output:
Enter the number of nodes: 5
Enter the data for node 1: 21
Enter the data for node 2: 36
Enter the data for node 3: 9
Enter the data for node 4: 36
Enter the data for node 5: 21

Linked List	21	36	9	36	21

### 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
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
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

• 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