Check if a linked list of strings forms a palindrome | Node class ;  ; A utility function to check if str is palindrome or not ; Match characters from beginning and end . ; Returns true if string formed by linked list is palindrome ; Append all nodes to form a string ; Utility function to print the linked LinkedList ; Driver program to test above function
class Node : NEW_LINE INDENT def __init__ ( self , data ) : NEW_LINE INDENT self . data = data NEW_LINE self . next = None NEW_LINE DEDENT DEDENT class LinkedList : NEW_LINE INDENT def __init__ ( self ) : NEW_LINE INDENT self . head = None NEW_LINE DEDENT def isPalindromeUtil ( self , string ) : NEW_LINE INDENT return ( string == string [ : : - 1 ] ) NEW_LINE DEDENT def isPalindrome ( self ) : NEW_LINE INDENT node = self . head NEW_LINE temp = [ ] NEW_LINE while ( node is not None ) : NEW_LINE INDENT temp . append ( node . data ) NEW_LINE node = node . next NEW_LINE DEDENT string = " " . join ( temp ) NEW_LINE return self . isPalindromeUtil ( string ) NEW_LINE DEDENT def printList ( self ) : NEW_LINE INDENT temp = self . head NEW_LINE while ( temp ) : NEW_LINE INDENT print temp . data , NEW_LINE temp = temp . next NEW_LINE DEDENT DEDENT DEDENT llist = LinkedList ( ) NEW_LINE llist . head = Node ( ' a ' ) NEW_LINE llist . head . next = Node ( ' bc ' ) NEW_LINE llist . head . next . next = Node ( " d " ) NEW_LINE llist . head . next . next . next = Node ( " dcb " ) NEW_LINE llist . head . next . next . next . next = Node ( " a " ) NEW_LINE print " true " if llist . isPalindrome ( ) else " false " NEW_LINE