C++ Program For Checking Linked List With A Loop Is Palindrome Or Not
Last Updated :
14 May, 2023
Given a linked list with a loop, the task is to find whether it is palindrome or not. You are not allowed to remove the loop.

Examples:
Input: 1 -> 2 -> 3 -> 2
/| |/
------- 1
Output: Palindrome
Linked list is 1 2 3 2 1 which is a
palindrome.
Input: 1 -> 2 -> 3 -> 4
/| |/
------- 1
Output: Not Palindrome
Linked list is 1 2 3 4 1 which is a
not palindrome.
Algorithm:
- Detect the loop using the Floyd Cycle Detection Algorithm.
- Then find the starting node of the loop as discussed in this.
- Check the linked list is palindrome or not as discussed in this.
Below is the implementation.
C++
// C++ program to check if a linked list
// with loop is palindrome or not.
#include<bits/stdc++.h>
using namespace std;
// Link list node
struct Node
{
int data;
struct Node * next;
};
/* Function to find loop starting node.
loop_node --> Pointer to one of the
loop nodes head --> Pointer to the
start node of the linked list */
Node* getLoopstart(Node *loop_node,
Node *head)
{
Node *ptr1 = loop_node;
Node *ptr2 = loop_node;
// Count the number of nodes in
// loop
unsigned int k = 1, i;
while (ptr1->next != ptr2)
{
ptr1 = ptr1->next;
k++;
}
// Fix one pointer to head
ptr1 = head;
// And the other pointer to k nodes
// after head
ptr2 = head;
for (i = 0; i < k; i++)
ptr2 = ptr2->next;
/* Move both pointers at the same pace,
they will meet at loop starting node */
while (ptr2 != ptr1)
{
ptr1 = ptr1->next;
ptr2 = ptr2->next;
}
return ptr1;
}
/* This function detects and find loop
starting node in the list*/
Node* detectAndgetLoopstarting(Node *head)
{
Node *slow_p = head, *fast_p = head,
*loop_start;
// Start traversing list and
// detect loop
while (slow_p && fast_p &&
fast_p->next)
{
slow_p = slow_p->next;
fast_p = fast_p->next->next;
/* If slow_p and fast_p meet then
find the loop starting node*/
if (slow_p == fast_p)
{
loop_start = getLoopstart(slow_p,
head);
break;
}
}
// Return starting node of loop
return loop_start;
}
// Utility function to check if a linked list
// with loop is palindrome with given starting
// point.
bool isPalindromeUtil(Node *head,
Node* loop_start)
{
Node *ptr = head;
stack<int> s;
// Traverse linked list until last node
// is equal to loop_start and store the
// elements till start in a stack
int count = 0;
while (ptr != loop_start || count != 1)
{
s.push(ptr->data);
if (ptr == loop_start)
count = 1;
ptr = ptr->next;
}
ptr = head;
count = 0;
// Traverse linked list until last node is
// equal to loop_start second time
while (ptr != loop_start || count != 1)
{
// Compare data of node with the top
// of stack
// If equal then continue
if (ptr->data == s.top())
s.pop();
// Else return false
else
return false;
if (ptr == loop_start)
count = 1;
ptr = ptr->next;
}
// Return true if linked list
// is palindrome
return true;
}
// Function to find if linked list
// is palindrome or not
bool isPalindrome(Node* head)
{
// Find the loop starting node
Node* loop_start =
detectAndgetLoopstarting(head);
// Check if linked list is palindrome
return isPalindromeUtil(head,
loop_start);
}
Node *newNode(int key)
{
Node *temp = new Node;
temp->data = key;
temp->next = NULL;
return temp;
}
// Driver code
int main()
{
Node *head = newNode(50);
head->next = newNode(20);
head->next->next = newNode(15);
head->next->next->next =
newNode(20);
head->next->next->next->next =
newNode(50);
// Create a loop for testing
head->next->next->next->next->next =
head->next->next;
isPalindrome(head)? cout <<
"Palindrome" : cout << "
Not Palindrome";
return 0;
}
Output:
Palindrome
Time Complexity: O(n) where n is no of nodes in the linked list
Auxiliary Space: O(n) where n is size of stack
Approach 2: Constant Space.
Here's the approach to check if a linked list is palindrome or not without using extra space:
- Find the middle node of the linked list using the slow and fast pointer approach.
- Reverse the second half of the linked list.
- Traverse both halves of the linked list (first half from start to middle, and second half from middle to end) and compare their data.
- If all the data is same, then the linked list is palindrome. Otherwise, it's not palindrome.
Here's the C++ code implementing the above approach:
C++
#include<bits/stdc++.h>
using namespace std;
// Link list node
struct Node
{
int data;
struct Node * next;
};
/* Function to reverse a linked list
from the given node */
Node* reverseList(Node *head)
{
Node *prev = NULL;
Node *curr = head;
Node *next;
while(curr != NULL)
{
next = curr->next;
curr->next = prev;
prev = curr;
curr = next;
}
return prev;
}
// Function to check if a linked list is palindrome or not
bool isPalindrome(Node* head)
{
// Find the middle node of the linked list
Node *slow = head, *fast = head;
while(fast != NULL && fast->next != NULL)
{
slow = slow->next;
fast = fast->next->next;
}
// Reverse the second half of the linked list
Node *rev_second_half = reverseList(slow);
// Traverse both halves of the linked list and compare their data
Node *p1 = head, *p2 = rev_second_half;
while(p2 != NULL)
{
if(p1->data != p2->data)
return false;
p1 = p1->next;
p2 = p2->next;
}
return true;
}
// Utility function to create a new node
Node *newNode(int key)
{
Node *temp = new Node;
temp->data = key;
temp->next = NULL;
return temp;
}
// Driver code
int main()
{
Node *head = newNode(1);
head->next = newNode(2);
head->next->next = newNode(3);
head->next->next->next = newNode(2);
head->next->next->next->next = newNode(1);
isPalindrome(head)? cout<<"Palindrome": cout<<"Not Palindrome";
return 0;
}
Time Complexity: O(n) where n is no of nodes in the linked list
Auxiliary Space: O(1) Uses no extra space.
Please refer complete article on Check linked list with a loop is palindrome or not for more details!
Similar Reads
C++ Program To Check If A Singly Linked List Is Palindrome Given a singly linked list of characters, write a function that returns true if the given list is a palindrome, else false. Recommended: Please solve it on "PRACTICE" first, before moving on to the solution. METHOD 1 (Use a Stack): A simple solution is to use a stack of list nodes. This mainly invol
9 min read
C++ Program to Check if a Given String is Palindrome or Not A string is said to be palindrome if the reverse of the string is the same as the original string. In this article, we will check whether the given string is palindrome or not in C++.ExamplesInput: str = "ABCDCBA"Output: "ABCDCBA" is palindromeExplanation: Reverse of the string str is "ABCDCBA". So,
4 min read
C++ Program For Detecting Loop In A Linked List Given a linked list, check if the linked list has loop or not. Below diagram shows a linked list with a loop. The following are different ways of doing this. Solution 1: Hashing Approach: Traverse the list one by one and keep putting the node addresses in a Hash Table. At any point, if NULL is reach
11 min read
C++ Program to Check Whether a Number is Palindrome or Not A palindrome number is a number that remains the same even if its digits are reversed. In this article, we will learn how to check a given number is a palindrome or not in C++.The easiest way to check if a number is a palindrome is to simply reverse the original number, then check if both numbers ar
4 min read
Program to check if an Array is Palindrome or not using STL in C++ Given an array, the task is to determine whether an array is a palindrome or not, using STL in C++. Examples: Input: arr[] = {3, 6, 0, 6, 3} Output: Palindrome Input: arr[] = {1, 2, 3, 4, 5} Output: Not Palindrome Approach: Get the reverse of the Array using reverse() method, provided in STL.Initial
2 min read
C++ Program For Finding The Length Of Longest Palindrome List In A Linked List Using O(1) Extra Space Given a linked list, find the length of the longest palindrome list that exists in that linked list. Examples: Input : List = 2->3->7->3->2->12->24 Output : 5 The longest palindrome list is 2->3->7->3->2 Input : List = 12->4->4->3->14 Output : 2 The longest
3 min read
Recursive program to check if number is palindrome or not Given a number, the task is to write a recursive function that checks if the given number is a palindrome or not. Examples: Input: 121Output: yes Input: 532Output: no The approach for writing the function is to call the function recursively till the number is wholly traversed from the back. Use a te
4 min read
C++ Program To Check Whether The Length Of Given Linked List Is Even Or Odd Given a linked list, the task is to make a function which checks whether the length of the linked list is even or odd. Examples: Input : 1->2->3->4->NULL Output : Even Input : 1->2->3->4->5->NULL Output : OddRecommended: Please solve it on "PRACTICE" first, before moving o
3 min read
C++ Program To Check If Two Linked Lists Are Identical Two Linked Lists are identical when they have the same data and the arrangement of data is also the same. For example, Linked lists a (1->2->3) and b(1->2->3) are identical. . Write a function to check if the given two linked lists are identical. Recommended: Please solve it on "PRACTICE
3 min read
C++ Program For Comparing Two Strings Represented As Linked Lists Given two strings, represented as linked lists (every character is a node in a linked list). Write a function compare() that works similar to strcmp(), i.e., it returns 0 if both strings are the same, 1 if the first linked list is lexicographically greater, and -1 if the second string is lexicograph
2 min read