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.
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.
- 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.
Time Complexity: O(n)
Auxiliary Space: O(n)
This article is contributed by Sahil Chhabra (akku). If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.