Learn basic concept of c , c++ and python programming in regularcodes.com
check palindrome in linked list loop
Write an efficient algorithm to, check palindrome of given linked list loop.
1) First need to detect loop of linked list. use two pointer first_ptr, and second_ptr. this both pointer start with first node of linked list. first_ptr are increment by one (visited next memory block) and second_ptr are increment by 2. If first_ptr are equal to second_ptr that means loop are exist in linked list.do step 2. Otherwise loop are not found.
2) In case loop are found then check palindrome of this loop.
Result : Not Palindrome
loop value is: [3,4,2,5,4,3] this is not palindrome.
Result : Palindrome.
loop value is: [3,4,5,5,4,3] this is palindrome.
View nodes and pointers
Program for check given linked list loop are contain palindrome or not . Time complexity O(n).
Execution Process Try it Yourself