Given a linked list of strings, check whether the concatenation of all values in the list together forms a palindrome. It is not permissible to construct a string out of the linked list nodes and check that string for palindrome.

For example,

Input:  AA —> XYZ —> CD —> C —> ZYX —> AA —> null
Output: true
Explanation: String AAXYZCDCZYXAA is palindrome
 
 
Input:  A —> B —> C —> DC —> B —> null
Output: false
Explanation: String ABCDCB is not a palindrome

Practice this problem

The idea is to traverse till the end of the linked list using recursion and construct a string that contains a concatenation of all values in the linked list nodes (in encountered order). When the recursion unfolds, we build another string containing a concatenation of all strings in reverse order. This time, the encountered order of linked list nodes is the opposite, i.e., from the last node towards the head node.

Now the problem reduces to just validating if both constructed strings are equal or not. If both strings are the same, we can say that the linked list is palindromic. The algorithm can be implemented as follows in C++, Java, and Python:

C++


Download  Run Code

Output:

Linked List [AA —> XYZ —> CD —> C —> ZYX —> AA —> nullptr] is a palindrome.

Java


Download  Run Code

Output:

Linked List [AA —> XYZ —> CD —> C —> ZYX —> AA —> null] is a palindrome.

Python


Download  Run Code

Output:

Linked List [AA —> XYZ —> CD —> C —> ZYX —> AA —> None] is a palindrome

The time complexity of the above solution is O(N.M), where N is the length of the linked list and M is the maximum word length. The auxiliary space required by the program for the call stack is proportional to the lists’ length.