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 Floyd Cycle Detection Algorithm.
- Then find the starting node of loop as discussed in this
- Check linked list is palindrome or not as discussed in this
Below is the implementation.
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 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.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Check if a doubly linked list of characters is palindrome or not
- Length of longest palindrome list in a linked list using O(1) extra space
- Check if a linked list is Circular Linked List
- Check if a linked list of strings forms a palindrome
- Function to check if a singly linked list is palindrome
- Find length of loop in linked list
- Detect loop in a linked list
- Find first node of loop in a linked list
- Find length of loop in a Linked List using Map
- Make a loop at k-th position in a linked list
- Detect and Remove Loop in a Linked List
- XOR Linked List - A Memory Efficient Doubly Linked List | Set 1
- XOR Linked List – A Memory Efficient Doubly Linked List | Set 2
- Merge a linked list into another linked list at alternate positions
- Convert singly linked list into circular linked list
- Difference between Singly linked list and Doubly linked list
- Convert Singly Linked List to XOR Linked List
- Create new linked list from two given linked list with greater element at each node
- Generate Linked List consisting of maximum difference of squares of pairs of nodes from given Linked List
- Sum of all Palindrome Numbers present in a Linked list