# Length of longest palindrome list in a linked list using O(1) extra space

Given a linked list, find length of the longest palindrome list that exist 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
```

## Recommended: Please solve it on “PRACTICE” first, before moving on to the solution.

A simple solution could be to copy linked list content to array and then find longest palindromic subarray in 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 given linked list and one by one reverse every prefix of linked list from left. After reversing a prefix, we find the longest common list beginning from reversed prefix and list after the reversed prefix.

Below is C++ implementation of above idea.

```// C++ program to find longest palindrome
// sublist in a list in O(1) time.
#include<bits/stdc++.h>
using namespace std;

//structure of the linked list
struct Node
{
int data;
struct Node* next;
};

// function for counting the common elements
int countCommon(Node *a, Node *b)
{
int count = 0;

// loop to count coomon in the list starting
// from node a and b
for (; a && b; 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
int maxPalindrome(Node *head)
{
int result = 0;
Node *prev = NULL, *curr = head;

// loop till the end of the linked list
while (curr)
{
// 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 = 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 = 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
Node *newNode(int key)
{
Node *temp = new Node;
temp->data = key;
temp->next = NULL;
return temp;
}

/* Drier program to test above functions*/
int main()
{
/* 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);

cout << maxPalindrome(head) << endl;
return 0;
}
```

Output :

`5`

Time Complexity : O(n2)

Note that the above code modifies the given linked list and may not work if modifications to linked list are not allowed. However we can finally do one more reverse to get 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 contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

# GATE CS Corner    Company Wise Coding Practice

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.
4 Average Difficulty : 4/5.0
Based on 32 vote(s)

Writing code in comment? Please use ide.geeksforgeeks.org, generate link and share the link here.