Java 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. Java // Java program to check if a linked list // with loop is palindrome or not. import java.util.*; class GfG{ // Link list node static class Node { int data; 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 */ static Node getLoopstart(Node loop_node, Node head) { Node ptr1 = loop_node; Node ptr2 = loop_node; // Count the number of nodes in // loop int k = 1, i; while (ptr1.next != ptr2) { ptr1 = ptr1.next; k++; } // Fix one pointer to head ptr1 = head; // And the other pointer to k // nodes after head ptr2 = 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*/ static Node detectAndgetLoopstarting(Node head) { Node slow_p = head, fast_p = head, loop_start = null; // Start traversing list and detect loop while (slow_p != null && fast_p != null && fast_p.next != null) { 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. static boolean isPalindromeUtil(Node head, Node loop_start) { Node ptr = head; Stack<Integer> s = new Stack<Integer> (); // 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; } ptr = head; 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.peek()) 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 static boolean isPalindrome(Node head) { // Find the loop starting node Node loop_start = detectAndgetLoopstarting(head); // Check if linked list is // palindrome return isPalindromeUtil(head, loop_start); } static Node newNode(int key) { Node temp = new Node(); temp.data = key; temp.next = null; return temp; } // Driver code public static void main(String[] args) { Node 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) System.out.println("Palindrome"); else System.out.println("Not Palindrome"); } } // This code is contributed by prerna saini Output: Palindrome Time Complexity: O(n) where n is no of nodes in the linked list Auxiliary Space: O(n) due to stack data structure 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 Java Program For Checking Linked List With A Loop Is Palindrome Or Not kartik Follow Improve Article Tags : Linked List Java Programs DSA palindrome Practice Tags : Linked Listpalindrome Similar Reads Java 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 Java Program to Check Whether a String is a Palindrome A string in Java can be called a palindrome if we read it from forward or backward, it appears the same or in other words, we can say if we reverse a string and it is identical to the original string for example we have a string s = "jahaj " and when we reverse it s = "jahaj"(reversed) so they look 8 min read Java Program To Check If Two Linked Lists Are Identical Two Linked Lists are identical when they have the same data and the arrangement of data is also the same. For example, Linked lists a (1->2->3) and b(1->2->3) are identical. . Write a function to check if the given two linked lists are identical. Recommended: Please solve it on "PRACTICE 3 min read Java Program to Reverse a Linked List Without Manipulating its Pointers Given a linked list, the task is to write a program in Java that reverses the linked list without manipulating its pointers, i.e., the reversal should happen just by changing the data values and not the links. ExamplesInput: Original linked list 1->2->3->4->5->nullOutput: Linked list 3 min read Java Program For Comparing Two Strings Represented As Linked Lists Given two strings, represented as linked lists (every character is a node in a linked list). Write a function compare() that works similar to strcmp(), i.e., it returns 0 if both strings are the same, 1 if the first linked list is lexicographically greater, and -1 if the second string is lexicograph 2 min read Palindrome Number Program in Java A given number can be said to be palindromic if the reverse of the given number is the same as that of a given number. In this article, we will write a Program to check if a number is a Palindrome Number in Java.Example of Palindrome Number:Input : n = 121Output: Reverse of n = 121Palindrome : YesIn 7 min read Java Program for Reverse a linked list Given a pointer to the head node of a linked list, the task is to reverse the linked list. We need to reverse the list by changing links between nodes. Examples: Input: Head of following linked list 1->2->3->4->NULLOutput: Linked list should be changed to, 4->3->2->1->NULL In 3 min read Java Program to Search an Element in a Linked List Prerequisite: LinkedList in java LinkedList is a linear data structure where the elements are not stored in contiguous memory locations. Every element is a separate object known as a node with a data part and an address part. The elements are linked using pointers or references. Linked Lists are pre 5 min read Iterate a LinkedList in Reverse Order in Java For traversing a linked list in reverse order we can use Descending Iterator or List Iterator 1. Descending Iterator Syntax: LinkedList<String> linkedlist = new LinkedList<>(); Iterator<String> listIterator = linkedlist.descendingIterator(); Returns: Descending Iterator returns the 2 min read Java Program to Search an Element in a Circular Linked List A linked list is a kind of linear data structure where each node has a data part and an address part which points to the next node. A circular linked list is a type of linked list where the last node points to the first one, making a circle of nodes. Example: Input : CList = 6->5->4->3-> 3 min read Like