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.
C++
#include<bits/stdc++.h>
using namespace std;
struct Node
{
int data;
struct Node* next;
};
int countCommon(Node *a, Node *b)
{
int count = 0;
for (; a && b; a = a->next, b = b->next)
if (a->data == b->data)
++count;
else
break ;
return count;
}
int maxPalindrome(Node *head)
{
int result = 0;
Node *prev = NULL, *curr = head;
while (curr)
{
Node *next = curr->next;
curr->next = prev;
result = max(result,
2*countCommon(prev, next)+1);
result = max(result,
2*countCommon(curr, next));
prev = curr;
curr = next;
}
return result;
}
Node *newNode( int key)
{
Node *temp = new Node;
temp->data = key;
temp->next = NULL;
return temp;
}
int main()
{
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);
cout << maxPalindrome(head) << endl;
return 0;
}
|
Java
class GfG
{
static class Node
{
int data;
Node next;
}
static int countCommon(Node a, Node b)
{
int count = 0 ;
for (; a != null && b != null ;
a = a.next, b = b.next)
if (a.data == b.data)
++count;
else
break ;
return count;
}
static int maxPalindrome(Node head)
{
int result = 0 ;
Node prev = null , curr = head;
while (curr != null )
{
Node next = curr.next;
curr.next = prev;
result = Math.max(result,
2 * countCommon(prev, next)+ 1 );
result = Math.max(result,
2 *countCommon(curr, next));
prev = curr;
curr = next;
}
return result;
}
static Node newNode( int key)
{
Node temp = new Node();
temp.data = key;
temp.next = null ;
return temp;
}
public static void main(String[] args)
{
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));
}
}
|
Python
class Node:
def __init__( self , data):
self .data = data
self . next = None
def countCommon(a, b) :
count = 0
while ( a ! = None and b ! = None ) :
if (a.data = = b.data) :
count = count + 1
else :
break
a = a. next
b = b. next
return count
def maxPalindrome(head) :
result = 0
prev = None
curr = head
while (curr ! = None ) :
next = curr. next
curr. next = prev
result = max (result,
2 * countCommon(prev, next ) + 1 )
result = max (result,
2 * countCommon(curr, next ))
prev = curr
curr = next
return result
def newNode(key) :
temp = Node( 0 )
temp.data = key
temp. next = None
return temp
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 )
print (maxPalindrome(head))
|
C#
using System;
class GfG
{
public class Node
{
public int data;
public Node next;
}
static int countCommon(Node a, Node b)
{
int count = 0;
for (; a != null && b != null ;
a = a.next, b = b.next)
if (a.data == b.data)
++count;
else
break ;
return count;
}
static int maxPalindrome(Node head)
{
int result = 0;
Node prev = null , curr = head;
while (curr != null )
{
Node next = curr.next;
curr.next = prev;
result = Math.Max(result,
2 * countCommon(prev, next)+1);
result = Math.Max(result,
2*countCommon(curr, next));
prev = curr;
curr = next;
}
return result;
}
static Node newNode( int key)
{
Node temp = new Node();
temp.data = key;
temp.next = null ;
return temp;
}
public static void Main(String []args)
{
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);
Console.WriteLine(maxPalindrome(head));
}
}
|
Javascript
<script>
class Node {
constructor() {
this .data = 0;
this .next = null ;
}
}
function countCommon(a, b) {
var count = 0;
for (; a != null && b != null ; a = a.next, b = b.next)
if (a.data == b.data)
++count;
else
break ;
return count;
}
function maxPalindrome(head) {
var result = 0;
var prev = null , curr = head;
while (curr != null ) {
var next = curr.next;
curr.next = prev;
result = Math.max(result, 2 *
countCommon(prev, next) + 1);
result = Math.max(result, 2 *
countCommon(curr, next));
prev = curr;
curr = next;
}
return result;
}
function newNode(key) {
var temp = new Node();
temp.data = key;
temp.next = null ;
return temp;
}
var 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);
document.write(maxPalindrome(head));
</script>
|
Time Complexity : O(n2)
Auxiliary Space: O(1), since no extra space is used.
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.
This article is contributed by Niteesh kumar. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.