Wednesday, November 20, 2024
Google search engine
HomeData Modelling & AIJava Program For Finding The Length Of Longest Palindrome List In A...

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->3->7->3->2->12->24
Output : 5
The longest palindrome list is 2->3->7->3->2

Input  : List = 12->4->4->3->14
Output : 2
The longest palindrome list is 4->4

 

A simple solution could be to copy linked list content to array and then find the longest palindromic subarray in the array, but this solution is not allowed as it requires extra space.
The idea is based on iterative linked list reverse process. We iterate through the given a linked list and one by one reverse every prefix of the linked list from the left. After reversing a prefix, we find the longest common list beginning from reversed prefix and the list after the reversed prefix. 
Below is the implementation of the above idea.
 

Java




// Java program to find longest palindrome
// sublist in a list in O(1) time.
class GfG
{
 
//structure of the linked list
static class Node
{
    int data;
    Node next;
}
 
// function for counting the common elements
static int countCommon(Node a, Node b)
{
    int count = 0;
 
    // loop to count common in the list starting
    // from node a and b
    for (; a != null && b != null;
            a = a.next, b = b.next)
 
        // increment the count for same values
        if (a.data == b.data)
            ++count;
        else
            break;
 
    return count;
}
 
// Returns length of the longest palindrome
// sublist in given list
static int maxPalindrome(Node head)
{
    int result = 0;
    Node prev = null, curr = head;
 
    // loop till the end of the linked list
    while (curr != null)
    {
        // The sublist from head to current
        // reversed.
        Node next = curr.next;
        curr.next = prev;
 
        // check for odd length
        // palindrome by finding
        // longest common list elements
        // beginning from prev and
        // from next (We exclude curr)
        result = Math.max(result,
                    2 * countCommon(prev, next)+1);
 
        // check for even length palindrome
        // by finding longest common list elements
        // beginning from curr and from next
        result = Math.max(result,
                    2*countCommon(curr, next));
 
        // update prev and curr for next iteration
        prev = curr;
        curr = next;
    }
    return result;
}
 
// Utility function to create a new list node
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)
{
    /* Let us create a linked lists to test
    the functions
    Created list is a: 2->4->3->4->2->15 */
    Node head = newNode(2);
    head.next = newNode(4);
    head.next.next = newNode(3);
    head.next.next.next = newNode(4);
    head.next.next.next.next = newNode(2);
    head.next.next.next.next.next = newNode(15);
 
    System.out.println(maxPalindrome(head));
}
}
 
// This code is contributed by
// Prerna Saini.


Output : 

5

Time Complexity : O(n2)
Note that the above code modifies the given linked list and may not work if modifications to the linked list are not allowed. However, we can finally do one more reverse to get an original list back. Please refer complete article on Length of longest palindrome list in a linked list using O(1) extra space for more details!

Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

Commit to GfG’s Three-90 Challenge! Purchase a course, complete 90% in 90 days, and save 90% cost click here to explore.

Last Updated :
18 Apr, 2022
Like Article
Save Article


Previous

<!–

8 Min Read | Java

–>


Next


<!–

8 Min Read | Java

–>

Share your thoughts in the comments

RELATED ARTICLES

Most Popular

Recent Comments