Python Program For Checking Linked List With A Loop Is Palindrome Or Not Last Updated : 19 Jul, 2022 Comments Improve Suggest changes Like Article Like Report 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. Python # Python3 program to check if a # linked list with loop is palindrome # or not. # Node class class Node: # Constructor to initialize the # node object def __init__(self, data): self.data = data self.next = None # 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 def getLoopstart(loop_node,head): ptr1 = loop_node ptr2 = loop_node # Count the number of nodes in # loop k = 1 i = 0 while (ptr1.next != ptr2): ptr1 = ptr1.next k = k + 1 # Fix one pointer to head ptr1 = head # And the other pointer to k # nodes after head ptr2 = head i = 0 while (i < k): ptr2 = ptr2.next i = i + 1 # 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 def detectAndgetLoopstarting(head): slow_p = head fast_p = head loop_start = None # Start traversing list and detect loop while (slow_p != None and fast_p != None and fast_p.next != None): 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. def isPalindromeUtil(head, loop_start): ptr = head s = [] # Traverse linked list until last node # is equal to loop_start and store the # elements till start in a stack count = 0 while (ptr != loop_start or count != 1): s.append(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 or count != 1): # Compare data of node with the top of stack # If equal then continue if (ptr.data == s[-1]): 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 def isPalindrome(head): # Find the loop starting node loop_start = detectAndgetLoopstarting(head) # Check if linked list is palindrome return isPalindromeUtil(head, loop_start) def newNode(key): temp = Node(0) temp.data = key temp.next = None return temp # Driver code 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 if(isPalindrome(head) == True): print("Palindrome") else: print("Not Palindrome") # This code is contributed by Arnab Kundu Output: Palindrome Time Complexity: O(n) where n is no of nodes in the linked list Auxiliary Space: O(n) Please refer complete article on Check linked list with a loop is palindrome or not for more details! Comment More infoAdvertise with us Next Article Python Program For Checking Linked List With A Loop Is Palindrome Or Not kartik Follow Improve Article Tags : Linked List Python Programs DSA palindrome Practice Tags : Linked Listpalindrome Similar Reads Python 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 6 min read Python Program To Check If A Linked List Of Strings Forms A Palindrome Given a linked list handling string data, check to see whether data is palindrome or not? Examples: Input: a -> bc -> d -> dcb -> a -> NULL Output: True String "abcddcba" is palindrome. Input: a -> bc -> d -> ba -> NULL Output: False String "abcdba" is not palindrome. Reco 2 min read Python Program to Check if a String is Palindrome or Not The task of checking if a string is a palindrome in Python involves determining whether a string reads the same forward as it does backward. For example, the string "madam" is a palindrome because it is identical when reversed, whereas "hello" is not. Using two pointer techniqueThis approach involve 3 min read Python program to check if number is palindrome (one-liner) In this article, we are given a number and we have to check whether the number is palindrome or not in one-liner code. The output will be True if it's a Palindrome number otherwise it would be False. Let's discuss how to find whether a number is palindrome or not in this article. Input1: test_number 3 min read Python Program For Finding The Length Of Loop In Linked List Write a function detectAndCountLoop() that checks whether a given Linked List contains loop and if loop is present then returns count of nodes in loop. For example, the loop is present in below-linked list and length of the loop is 4. If the loop is not present, then the function should return 0. Re 4 min read Python Program to find all Palindromic Bitlists in length In this article, we will learn to generate all Palindromic Bitlists of a given length using Backtracking in Python. Backtracking can be defined as a general algorithmic technique that considers searching every possible combination in order to solve a computational problem. Whereas, Bitlists are list 6 min read Python 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 4 min read Python program to check if given string is vowel Palindrome Given a string (may contain both vowel and consonant letters), remove all consonants, then check if the resulting string is palindrome or not. Examples: Input : abcuhuvmnba Output : YES Explanation : The consonants in the string "abcuhuvmnba" are removed. Now the string becomes "auua". Input : xayzu 5 min read Python Program To Find Minimum Insertions To Form A Palindrome | DP-28 Given string str, the task is to find the minimum number of characters to be inserted to convert it to a palindrome. Before we go further, let us understand with a few examples: ab: Number of insertions required is 1 i.e. babaa: Number of insertions required is 0 i.e. aaabcd: Number of insertions re 9 min read Like