Open In App

Javascript Program For Checking Linked List With A Loop Is Palindrome Or Not

Last Updated : 03 Sep, 2024
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:

  1. Detect the loop using the Floyd Cycle Detection Algorithm.
  2. Then find the starting node of the loop as discussed in this.
  3. Check the linked list is palindrome or not as discussed in this.

Below is the implementation. 


Output
Palindrome

Complexity Analysis:

  • Time Complexity: O(n) where n is no of nodes in the linked list
  • Auxiliary Space: O(n) where n is size of List

Please refer complete article on Check linked list with a loop is palindrome or not for more details!


Next Article

Similar Reads