Skip to content
Related Articles

Related Articles

Python Program To Check If A Linked List Of Strings Forms A Palindrome

View Discussion
Improve Article
Save Article
Like Article
  • Last Updated : 22 Jun, 2022

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

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

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

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


# 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): = data = None
class LinkedList:
    # Function to initialize head
    def __init__(self):
        self.head = None
    # 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):
        node = self.head
        # Append all nodes to form a string
        temp = []
        while (node is not None):
            node =
        string = "".join(temp)
        return self.isPalindromeUtil(string)
    # Utility function to print the
    # linked LinkedList
    def printList(self):
        temp = self.head
        while (temp):
            temp =
# Driver code
llist = LinkedList()
llist.head = Node('a') = Node('bc') = Node("d") = Node("dcb") = Node("a")
print "true" if llist.isPalindrome() else "false"
# This code is contributed by Nikhil Kumar Singh(nickzuck_007)



Time Complexity: O(n), where n is the number of nodes in the given linked list.
Auxiliary Space: O(m), where m is the length of the string formed by the linked list.

Please refer complete article on Check if a linked list of strings forms a palindrome for more details!

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!