# Length of longest palindrome in linked list

Given an linked list which are contain n nodes. Our goal is to find the largest length palindrome which are exist in this linked list. Note that there can be possible more than one palindrome in linked list.

In above image there are more than one palindrome are possible.

``````3  2  2  3 [Length 4]
2  2 [Length 2]
2  3  1  1  3  2 [Length 6]
3  1  1  3 [Length 4]
``````

Here given code implementation process.

``````//C Program
//Length of longest palindrome in linked list
#include<stdio.h>
#include <stdlib.h> //for malloc function

//Create structure of linled list node
struct Node
{
int data;
struct Node*next;
};

//Insert Linked list node at the end
{
//Create dynamic node
struct Node*node=(struct Node*)malloc(
sizeof(struct Node)
);
if(node==NULL)
{
printf("Memory overflow\n");

}else
{
node->data=value;
node->next=NULL;
{

}else
{
//Find last node
while(temp->next!=NULL)
{
temp=temp->next;
}
temp->next=node;
}
}
}

void palindrome(struct Node**back,
struct Node*end,
struct Node*terminate,
int *status)
{
if(*status!=1 || end==terminate)
{
return;
}
palindrome(back,end->next,terminate,status);

if((*back)->data != end->data )
{
//When data are not similar, then this not a palindrome
*status=0;
}
else
{
(*back)=(*back)->next;
}
}

{
//Setup the initial pointer which is controlling execution
*inner=NULL,
*temp=NULL,
*show=NULL;

//resultant variable
int result=0,status=0,count=0;

while(outer!=NULL)
{

count=1;

inner=outer->next;

while(inner!=NULL)
{

count++;

if(inner->data == outer->data && count > result)
{
//Assume palindrome exist
status=1;

temp=outer;

//find palindrome between two linked list node
palindrome(&temp,outer,inner->next,&status);

if(status==1 && result < count)
{
//When gets a new longest length palindrome
//Get address of starting element of palindrome
show=outer;

//Get the distance of two nodes
result = count;
}
}
inner=inner->next;
}
outer=outer->next;
}
if(result>0)
{
//When palindrome exist then show there sequence and result
printf("\nResult : %d \n{",result);

while(show!=NULL && result >0)
{
//Display element sequence
printf("%3d",show->data);

result--;

show = show->next;
}

printf(" }\n");
}
else
{
//In case each element is a palindrome
printf("\n Result : %d",1);
}
}

//Display element of Node
void display(struct Node*temp)
{

if(temp==NULL)
{
}

while(temp!=NULL)
{
printf("%d  ",temp->data);
temp=temp->next;
}

}
int main()
{
//create node pointer

//display all node

return 0;
}``````

#### Output

``````3  2  2  3  1  1  3  2
Result : 6
{  2  3  1  1  3  2 }``````
``````//C++ Program
//Length of longest palindrome in linked list
#include <iostream>
using namespace std;

//Create structure of linled list node
struct Node
{
int data;
struct Node*next;
};
{
public:
void insert(int);
void display();
void find();
void palindrome(Node**, Node*, Node*,int *);
};
{
}

//insert node at end of linked list
{
//Create dynamic node
struct Node*node=new Node;
if(node==NULL)
{
cout<<"Memory overflow\n";
}else
{
node->data=value;
node->next=NULL;
{
//base condition
}else
{
while(temp->next!=NULL)
{
temp=temp->next;
}
temp->next=node;
}
}
}
Node*end,
Node*terminate,
int *status)
{
if(*status!=1 || end==terminate)
{
return;
}
palindrome(back,end->next,terminate,status);

if((*back)->data != end->data )
{
//When data are not similar, then this not a palindrome
*status=0;
}
else
{
(*back)=(*back)->next;
}
}

{
//Setup the initial pointer which is controlling execution
*inner=NULL,
*temp=NULL,
*show=NULL;

//resultant variable
int result=0,status=0,count=0;

while(outer!=NULL)
{

count=1;

inner=outer->next;

while(inner!=NULL)
{

count++;

if(inner->data == outer->data && count > result)
{
//Assume palindrome exist
status=1;

temp=outer;

//find palindrome between two linked list node
palindrome(&temp,outer,inner->next,&status);

if(status==1 && result < count)
{
//When gets a new longest length palindrome
//Get address of starting element of palindrome
show=outer;

//Get the distance of two nodes
result = count;
}
}
inner=inner->next;
}
outer=outer->next;
}
if(result>0)
{
//When palindrome exist then show there sequence and result
cout<<"\nResult : "<< result <<"\n{";

while(show!=NULL && result >0)
{
//Display element sequence
cout<<" "<<show->data;

result--;

show = show->next;
}

cout<<(" }\n");
}
else
{
//In case each element is a palindrome
cout<<"\n Result : "<<1;
}
}
//Display all node value in linked list
{
{
}
else
{

while(temp!=NULL)
{
//print node value
cout<<temp->data<<" ";

temp=temp->next;
}
}

}
int main(){

//create object

obj.insert(3);
obj.insert(2);
obj.insert(2);
obj.insert(3);
obj.insert(1);
obj.insert(1);
obj.insert(3);
obj.insert(2);

//display all node
obj.display();

obj.find();

return 0;
}``````

#### Output

``````Linked List : 3 2 2 3 1 1 3 2
Result : 6
{ 2 3 1 1 3 2 }
``````
``````//Java Program
//Length of longest palindrome in linked list
public class MySLL
{

static class Node
{
int data;
Node next;
Node(int data)
{
this.data = data;
this.next = null;
}
}
//This are using to control execution process
public int status;
//Class constructors
MySLL()
{
status = 1;
checker=null;
}
//insert element
public void insert(int value)
{
//Create  node
Node node=new Node(value);

{
}
else
{
//find last node
while(temp.next!=null)
{
temp=temp.next;
}
temp.next=node;
}
}

public void display()
{
{

while(temp!=null)
{
System.out.print("  "+temp.data);

temp=temp.next;
}
System.out.println();
}else
{
}
}
public void  palindrome(Node end,Node terminate)
{
if( status!=1 || end==terminate)
{
return;
}
palindrome(end.next,terminate);

if(checker.data != end.data )
{
//When data are not similar, then this not a palindrome
status=0;
}
else
{
checker=checker.next;
}
}

public void find()
{
{
return;
}

//Setup the initial obj which is controlling execution
inner=null,
show=null;

//resultant variable
int result=0,count=0;

while(outer!=null)
{

count=1;

inner=outer.next;

while(inner!=null)
{

count++;

if(inner.data == outer.data && count > result)
{
//Assume palindrome exist
status=1;

//set instance object variable value
checker=outer;

//find palindrome between two linked list node
palindrome(outer,inner.next);

if(status==1 && result < count)
{
//When gets a new longest length palindrome
//Get address of starting element of palindrome
show=outer;

//Get the distance of two nodes
result = count;
}
}

inner=inner.next;
}
outer=outer.next;
}
if(result>0)
{
//When palindrome exist then show there sequence and result

System.out.print("Result : "+result+"\n{");

while(show!=null && result >0)
{
//Display element sequence

System.out.print(" "+show.data);
result--;

show = show.next;
}

System.out.println(" }");
}
else
{
//In case each element is a palindrome

System.out.println("\nResult : 1");
}
}
public static void main(String[] args) {

MySLL obj=new MySLL();

obj.insert(3);
obj.insert(2);
obj.insert(2);
obj.insert(3);
obj.insert(1);
obj.insert(1);
obj.insert(3);
obj.insert(2);

//display all node
obj.display();

obj.find();
}
}``````

#### Output

``````Linked List Element :  3  2  2  3  1  1  3  2
Result : 6
{ 2 3 1 1 3 2 }
``````
``````# Python Program
# Length of longest palindrome in linked list
class Node:

def __init__(self,data):
self.data=data
self.next=None

class MySLL:

def __init__(self):
self.status = 1
self.checker = None

#insert new node to linked list
def insert(self,data):

node=Node(data)

else:

while temp.next!=None:
temp=temp.next

temp.next=node

def display(self):

return

while(temp!=None):

print(temp.data,end="  ")

temp=temp.next

print("\n")

def  palindrome(self, end, terminate):
if( self.status!=1 or end==terminate):
return;

self.palindrome(end.next,terminate);

if(self.checker.data != end.data ):
#When data are not similar, then this not a palindrome
self.status=0;

else:
self.checker=self.checker.next;

def find(self):

return;

#Setup the initial obj which is controlling execution
inner=None
show=None

#resultant variable
result=0
count=0;

while(outer!=None):

count=1;

inner=outer.next;

while(inner!=None):

count+=1;

if(inner.data == outer.data and count > result):
#Assume palindrome exist
self.status=1;

#set instance object variable value
self.checker = outer;

#find palindrome between two linked list
self.palindrome(outer,inner.next);

if(self.status==1 and result < count):

#When gets a new longest length palindrome
#Get address of starting element of palindrome
show = outer;

#Get the distance of two s
result = count;

inner=inner.next;

outer=outer.next;

if(result>0):
#When palindrome exist then show there sequence and result

print("Result : ",end="");
print(result,end="")
print("\n{",end="");

while(show!=None and result >0):
#Display element sequence

print(" ",show.data,end ="");
result-=1;

show = show.next;

print(" }");

else:
#In case each element is a palindrome
print("\nResult : 1");

def main():

#Create object
obj=MySLL();

obj.insert(3);
obj.insert(2);
obj.insert(2);
obj.insert(3);
obj.insert(1);
obj.insert(1);
obj.insert(3);
obj.insert(2);

#display all node
obj.display();

obj.find();

if __name__=="__main__":
main()``````

#### Output

``````Linked List Elements :
3  2  2  3  1  1  3  2

Result : 6
{  2  3  1  1  3  2 }
``````
``````//C# Program
//Palindromic partitions of a string
using System;

public class Node
{
public int data;
public Node next;
public Node(int data)
{
this.data = data;
this.next = null;
}
}
public class MySLL
{

//This are using to control execution process
public int status;
//Class constructors
MySLL()
{
this.status = 1;
this.checker = null;
}
//insert element
public void insert(int value)
{
//Create  node
Node node=new Node(value);

{
}
else
{
//find last node
while(temp.next!=null)
{
temp=temp.next;
}
temp.next=node;
}
}

public void display()
{
{

while(temp!=null)
{
Console.Write("  {0}",temp.data);

temp=temp.next;
}
Console.WriteLine();
}else
{
}
}
public void  palindrome(Node end,Node terminate)
{
if( this.status!=1 || end == terminate)
{
return;
}
palindrome(end.next,terminate);

if(this.checker.data != end.data )
{
//When data are not similar, then this not a palindrome
this.status=0;
}
else
{
this.checker=this.checker.next;
}
}

public void find()
{
{
return;
}

//Setup the initial obj which is controlling execution
inner=null,
show=null;

//resultant variable
int result=0,count=0;

while(outer!=null)
{

count=1;

inner=outer.next;

while(inner!=null)
{

count++;

if(inner.data == outer.data && count > result)
{
//Assume palindrome exist
this.status=1;

//set instance object variable value
this.checker=outer;

//find palindrome between two linked list node
palindrome(outer,inner.next);

if(status==1 && result < count)
{
//When gets a new longest length palindrome
//Get address of starting element of palindrome
show=outer;

//Get the distance of two nodes
result = count;
}
}

inner=inner.next;
}
outer=outer.next;
}
if(result>0)
{
//When palindrome exist then show there sequence and result

Console.WriteLine("Result : {0} ",result);
Console.Write("  {");
while(show!=null && result >0)
{
//Display element sequence

Console.Write(" {0}",show.data);
result--;

show = show.next;
}

Console.Write(" }");

}
else
{
//In case each element is a palindrome

Console.Write("\nResult : 1");
}
}

public static void Main(String[] args) {

MySLL obj=new MySLL();

obj.insert(3);
obj.insert(2);
obj.insert(2);
obj.insert(3);
obj.insert(1);
obj.insert(1);
obj.insert(3);
obj.insert(2);

//display all node
obj.display();

obj.find();

}
}``````

#### Output

``````Linked List Element :
3  2  2  3  1  1  3  2
Result : 6
{ 2 3 1 1 3 2 }
``````
Length of longest palindrome in linked list

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.