# Check linked list with a loop is palindrome or not

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: Palindrome
Linked list is 1 2 3 4 1 which is a
not palindrome.
```

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Algorithm:

1. Detect the loop using Floyd Cycle Detection Algorithm.
2. Then find the starting node of loop as discussed in this
3. Check linked list is palindrome or not as discussed in this

Below is C++ implementation.

```// C++ program to check if a linked list with
// loop is palindrome or not.
#include<bits/stdc++.h>
using namespace std;

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

// And the other pointer to k nodes after 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*/
{

//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)
{
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.
{
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;
}
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
{
// Find the loop starting node

// Check if linked list is palindrome
}

Node *newNode(int key)
{
Node *temp = new Node;
temp->data = key;
temp->next = NULL;
return temp;
}

/* Driver program to test above function*/
int main()
{

/* Create a loop for testing */

: cout << "\nNot Palindrome";

return 0;
}
```

Output:

```Palindrome
```

This article is contributed by Sahil Chhabra (akku). If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

# GATE CS Corner    Company Wise Coding Practice

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.
2 Average Difficulty : 2/5.0
Based on 12 vote(s)