Open In App

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

Last Updated : 19 Jul, 2022
Improve
Improve
Like Article
Like
Save
Share
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. 

Javascript




<script>
// Javascript program to check if a
// linked list with loop is palindrome
// or not.
class GfG{
 
// Link list node
class Node
{
    constructor()
    {
        this.data = 0;
        this.next = null;
    }
}
 
/* 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 */
function getLoopstart(loop_node, head)
{
    var ptr1 = loop_node;
    var ptr2 = loop_node;
 
    // Count the number of nodes in
    // loop
    var 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 */
function detectAndgetLoopstarting(head)
{
    var 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.
function isPalindromeUtil(head, loop_start)
{
    var ptr = head;
    var s =  [];
 
    // Traverse linked list until last node
    // is equal to loop_start and store the
    // elements till start in a stack
    var 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
        var stk = s.pop();
        if (ptr.data == stk);               
 
        // Else return false
        else
        {
            s.push(stk);
            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
function isPalindrome(head)
{
    // Find the loop starting node
    var loop_start =
        detectAndgetLoopstarting(head);
 
    // Check if linked list is palindrome
    return isPalindromeUtil(head,
                            loop_start);
}
 
function newNode(key)
{
    var temp = new Node();
    temp.data = key;
    temp.next = null;
    return temp;
}
 
// Driver code
var 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)
    document.write("Palindrome");
else
    document.write("Not Palindrome");
// This code is contributed by aashish1995
</script>


Output:  

Palindrome

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!



Previous Article
Next Article

Similar Reads

C++ Program For Checking Linked List With A Loop Is Palindrome Or Not
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 -&gt; 2 -&gt; 3 -&gt; 2 /| |/ ------- 1 Output: Palindrome Linked list is 1 2 3 2 1 which is a palindrome. Input: 1 -&gt; 2 -&gt; 3 -&gt; 4 /| |/ ------- 1 Output: Not Palindrome Linked list is 1 2 3 4 1 w
5 min read
Java Program For Checking Linked List With A Loop Is Palindrome Or Not
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 -&gt; 2 -&gt; 3 -&gt; 2 /| |/ ------- 1 Output: Palindrome Linked list is 1 2 3 2 1 which is a palindrome. Input: 1 -&gt; 2 -&gt; 3 -&gt; 4 /| |/ ------- 1 Output: Not Palindrome Linked list is 1 2 3 4 1 w
4 min read
Python Program For Checking Linked List With A Loop Is Palindrome Or Not
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 -&gt; 2 -&gt; 3 -&gt; 2 /| |/ ------- 1 Output: Palindrome Linked list is 1 2 3 2 1 which is a palindrome. Input: 1 -&gt; 2 -&gt; 3 -&gt; 4 /| |/ ------- 1 Output: Not Palindrome Linked list is 1 2 3 4 1 w
4 min read
Check linked list with a loop is palindrome or not
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 -&gt; 2 -&gt; 3 -&gt; 2 /|\ \|/ ------- 1 Output: Palindrome Linked list is 1 2 3 2 1 which is a palindrome. Input : 1 -&gt; 2 -&gt; 3 -&gt; 4 /|\ \|/ ------- 1 Output: Not Palindrome Linked list is 1 2 3
14 min read
Javascript Program For Finding The Length Of Longest Palindrome List In A Linked List Using O(1) Extra Space
Given a linked list, find the length of the longest palindrome list that exists in that linked list. Examples: Input : List = 2-&gt;3-&gt;7-&gt;3-&gt;2-&gt;12-&gt;24 Output : 5 The longest palindrome list is 2-&gt;3-&gt;7-&gt;3-&gt;2 Input : List = 12-&gt;4-&gt;4-&gt;3-&gt;14 Output : 2 The longest palindrome list is 4-&gt;4 Recommended: Please sol
3 min read
Javascript 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. Recommended: Please try your approach on PRACTICE, b
3 min read
JavaScript Program to Remove Loop in a Linked List
A linked list is a collection of nodes that contain both a data element and a reference (or pointer) to the next node in the collection. A common concern with linked lists is having loops and cycles. When one of the nodes in the linked list points towards any of its previous nodes leading to an infinite cycle. While traversing the list, this can ca
7 min read
Online algorithm for checking palindrome in a stream
Given a stream of characters (characters are received one by one), write a function that prints 'Yes' if a character makes the complete string palindrome, else prints 'No'. Examples: Input: str[] = "abcba"Output: a Yes // "a" is palindrome b No // "ab" is not palindrome c No // "abc" is not palindrome b No // "abcb" is not palindrome a Yes // "abcb
17 min read
C++ Program For Finding The Length Of Longest Palindrome List In A Linked List Using O(1) Extra Space
Given a linked list, find the length of the longest palindrome list that exists in that linked list. Examples: Input : List = 2-&gt;3-&gt;7-&gt;3-&gt;2-&gt;12-&gt;24 Output : 5 The longest palindrome list is 2-&gt;3-&gt;7-&gt;3-&gt;2 Input : List = 12-&gt;4-&gt;4-&gt;3-&gt;14 Output : 2 The longest palindrome list is 4-&gt;4Recommended: Please solv
3 min read
Java Program For Finding The Length Of Longest Palindrome List In A Linked List Using O(1) Extra Space
Given a linked list, find the length of the longest palindrome list that exists in that linked list. Examples: Input : List = 2-&gt;3-&gt;7-&gt;3-&gt;2-&gt;12-&gt;24 Output : 5 The longest palindrome list is 2-&gt;3-&gt;7-&gt;3-&gt;2 Input : List = 12-&gt;4-&gt;4-&gt;3-&gt;14 Output : 2 The longest palindrome list is 4-&gt;4 Recommended: Please sol
3 min read