# Check if a linked list of strings forms a palindrome

Given a linked list handling string data, check to see whether data is palindrome or not?
For example,

```Input  : a -> bc -> d -> dcb -> a -> NULL
Output : True
String "abcddcba" is palindrome.

Output : a -> bc -> d -> ba -> NULL
Output : False
String "abcdba" is not palindrome. ```

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

The idea is very simple. Construct a string out of given linked list and check if the constructed string is palindrome or not.

## C/C++

```// Program to check if a given linked list of strings
// form a palindrome
#include <bits/stdc++.h>
using namespace std;

struct Node
{
string data;
Node* next;
};

// A utility function to check if str is palindrome
// or not
bool isPalindromeUtil(string str)
{
int length = str.length();

// Match characters from beginning and
// end.
for (int i=0; i<length/2; i++)
if (str[i] != str[length-i-1])
return false;

return true;
}

// Returns true if string formed by linked
// list is palindrome
bool isPalindrome(Node *node)
{
// Append all nodes to form a string
string str = "";
while (node != NULL)
{
str.append(node->data);
node = node->next;
}

// Check if the formed string is palindrome
return isPalindromeUtil(str);
}

// A utility function to print a given linked list
void printList(Node *node)
{
while (node != NULL)
{
cout << node->data << " -> ";
node = node->next;
}
printf("NULL\n");
}

/* Function to create a new node with given data */
Node *newNode(const char *str)
{
Node *new_node = new Node;
new_node->data = str;
new_node->next = NULL;
return new_node;
}

/* Driver program to test above function*/
int main()
{

printf("false\n");

return 0;
}
```

## Java

```// Java Program to check if a given linked list of strings
// form a palindrome

import java.util.Scanner;

class Node
{
String data;
Node next;

Node(String d)
{
data = d;
next = null;
}
}

{

// A utility function to check if str is palindrome
// or not
boolean isPalidromeUtil(String str)
{
int length = str.length();

// Match characters from beginning and
// end.
for (int i=0; i<length/2; i++)
if (str.charAt(i) != str.charAt(length-i-1))
return false;

return true;
}

// Returns true if string formed by linked
// list is palindrome
boolean isPalindrome()
{

// Append all nodes to form a string
String str = "";
while (node != null)
{
str = str.concat(node.data);
node = node.next;
}

// Check if the formed string is palindrome
return isPalidromeUtil(str);
}

/* Driver program to test above function*/
public static void main(String[] args)
{

System.out.println(list.isPalindrome());

}
}
// This code has been contributed by Amit Khandelwal
```

## Python

```# Python program to check if given linked list of
# strings form a palindrome

# Node class
class Node:

# Constructor to initialize the node object
def __init__(self, data):
self.data = data
self.next = None

def __init__(self):

# A utility function to check if str is palindrome
# or not
def isPalindromeUtil(self, string):
return (string == string[::-1])

# Returns true if string formed by linked list is
# palindrome
def isPalindrome(self):

# Append all nodes to form a string
temp = []
while (node is not None):
temp.append(node.data)
node = node.next
string = "".join(temp)
return self.isPalindromeUtil(string)

def printList(self):
while (temp):
print temp.data,
temp = temp.next

# Driver program to test above function
print "true" if llist.isPalindrome() else "false"

# This code is contributed by Nikhil Kumar Singh(nickzuck_007)

```

Output:
`true`

# GATE CS Corner    Company Wise Coding Practice

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