Given a linked list of strings, check whether 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: String AAXYZCDCZYXAA is palindrome





Input: A -> B -> C -> DC -> B -> null

Output: String ABCDCB is not a palindrome





The idea is to traverse till the end of the linked list using recursion and construct a string which contains concatenation of all values in the linked list nodes (in encountered order). Then when the recursion unfolds, we construct another string which contains concatenation of all strings in reverse order. This time, the encountered order of linked list nodes is opposite. i.e. from 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 same, we can say that the linked list is palindromic.

C++ #include <iostream> #include <string> #include <algorithm> using namespace std; // Data Structure to store a linked list node struct Node { string data; Node* next; // Constructor Node(string str) { this->data = str; this->next = nullptr; } }; // Function to print a linked list void printList(Node* node) { while (node) { cout << node->data << " -> "; node = node->next; } cout << "null"; } // Function to reverse a string string reverse(string const &s) { string rev = ""; for_each(s.rbegin(), s.rend(), [&rev] (char const &c) { rev = rev.append(1, c); }); return rev; } // Construct string s1 and s2 out of given linked list with consecutive // elements of the list in forward and backward direction respectively void construct(Node* &head, string &s1, string &s2) { // Base case if (head == nullptr) return; s1 += head->data; construct(head->next, s1, s2); s2 += reverse(head->data); } // Function to check if a given linked list of strings is palindromic bool isPalindromic(Node *head) { // construct string s1 with consecutive elements of the linked list // construct string s2 by reversing consecutive elements of the linked list // starting from the end string s1, s2; construct(head, s1, s2); // check if linked list is palindromic return s1 == s2; } int main() { Node *head = new Node("AA"); head->next = new Node("XYZ"); head->next->next = new Node("CD"); head->next->next->next = new Node("C"); head->next->next->next->next = new Node("ZYX"); head->next->next->next->next->next = new Node("AA"); cout << "Linked List "; printList(head); if (isPalindromic(head)) cout << " is a palindrome."; else cout << " is not a palindrome."; return 0; } 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 #include <iostream> #include <string> #include <algorithm> using namespace std ; // Data Structure to store a linked list node struct Node { string data ; Node * next ; // Constructor Node ( string str ) { this -> data = str ; this -> next = nullptr ; } } ; // Function to print a linked list void printList ( Node * node ) { while ( node ) { cout << node -> data << " -> " ; node = node -> next ; } cout << "null" ; } // Function to reverse a string string reverse ( string const &s ) { string rev = "" ; for_each ( s . rbegin ( ) , s . rend ( ) , [ &rev ] ( char const &c ) { rev = rev . append ( 1 , c ) ; } ) ; return rev ; } // Construct string s1 and s2 out of given linked list with consecutive // elements of the list in forward and backward direction respectively void construct ( Node * &head , string &s1 , string &s2 ) { // Base case if ( head == nullptr ) return ; s1 += head -> data ; construct ( head -> next , s1 , s2 ) ; s2 += reverse ( head -> data ) ; } // Function to check if a given linked list of strings is palindromic bool isPalindromic ( Node * head ) { // construct string s1 with consecutive elements of the linked list // construct string s2 by reversing consecutive elements of the linked list // starting from the end string s1 , s2 ; construct ( head , s1 , s2 ) ; // check if linked list is palindromic return s1 == s2 ; } int main ( ) { Node * head = new Node ( "AA" ) ; head -> next = new Node ( "XYZ" ) ; head -> next -> next = new Node ( "CD" ) ; head -> next -> next -> next = new Node ( "C" ) ; head -> next -> next -> next -> next = new Node ( "ZYX" ) ; head -> next -> next -> next -> next -> next = new Node ( "AA" ) ; cout << "Linked List " ; printList ( head ) ; if ( isPalindromic ( head ) ) cout << " is a palindrome." ; else cout << " is not a palindrome." ; return 0 ; } Download Run Code Output:



Linked List AA -> XYZ -> CD -> C -> ZYX -> AA -> null is a palindrome.

Java // Data Structure to store a linked list node class Node { String data; Node next; // Constructor Node(String str) { this.data = str; this.next = null; } } class Main { // Function to print a linked list public static void printList(Node node) { while (node != null) { System.out.print(node.data + " -> "); node = node.next; } System.out.print("null"); } // Construct String s1 and s2 out of given linked list with consecutive // elements of the list in forward and backward direction respectively public static void construct(Node head, StringBuilder s1, StringBuilder s2) { // Base case if (head == null) { return; } s1.append(head.data); construct(head.next, s1, s2); s2.append(new StringBuilder(head.data).reverse().toString()); } // Function to check if a given linked list of Strings is palindromic public static boolean isPalindromic(Node head) { // construct String s1 with consecutive elements of the linked list // construct String s2 by reversing consecutive elements of the linked list // starting from the end StringBuilder s1 = new StringBuilder(), s2 = new StringBuilder(); construct(head, s1, s2); // check if linked list is palindromic return s1.toString().equals(s2.toString()); } public static void main(String[] args) { Node head = new Node("AA"); head.next = new Node("XYZ"); head.next.next = new Node("CD"); head.next.next.next = new Node("C"); head.next.next.next.next = new Node("ZYX"); head.next.next.next.next.next = new Node("AA"); System.out.print("Linked List "); printList(head); if (isPalindromic(head)) { System.out.print(" is a palindrome."); } else { System.out.print(" is not a palindrome."); } } } 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 // Data Structure to store a linked list node class Node { String data ; Node next ; // Constructor Node ( String str ) { this . data = str ; this . next = null ; } } class Main { // Function to print a linked list public static void printList ( Node node ) { while ( node != null ) { System . out . print ( node . data + " -> " ) ; node = node . next ; } System . out . print ( "null" ) ; } // Construct String s1 and s2 out of given linked list with consecutive // elements of the list in forward and backward direction respectively public static void construct ( Node head , StringBuilder s1 , StringBuilder s2 ) { // Base case if ( head == null ) { return ; } s1 . append ( head . data ) ; construct ( head . next , s1 , s2 ) ; s2 . append ( new StringBuilder ( head . data ) . reverse ( ) . toString ( ) ) ; } // Function to check if a given linked list of Strings is palindromic public static boolean isPalindromic ( Node head ) { // construct String s1 with consecutive elements of the linked list // construct String s2 by reversing consecutive elements of the linked list // starting from the end StringBuilder s1 = new StringBuilder ( ) , s2 = new StringBuilder ( ) ; construct ( head , s1 , s2 ) ; // check if linked list is palindromic return s1 . toString ( ) . equals ( s2 . toString ( ) ) ; } public static void main ( String [ ] args ) { Node head = new Node ( "AA" ) ; head . next = new Node ( "XYZ" ) ; head . next . next = new Node ( "CD" ) ; head . next . next . next = new Node ( "C" ) ; head . next . next . next . next = new Node ( "ZYX" ) ; head . next . next . next . next . next = new Node ( "AA" ) ; System . out . print ( "Linked List " ) ; printList ( head ) ; if ( isPalindromic ( head ) ) { System . out . print ( " is a palindrome." ) ; } else { System . out . print ( " is not a palindrome." ) ; } } } Download Run Code Output:



Linked List AA -> XYZ -> CD -> C -> ZYX -> AA -> null is a palindrome.

Python # Data Structure to store a linked list node class Node: def __init__(self, str): self.data = str self.next = None # Function to print a linked list def printList(node): while node: print(node.data, end=" -> ") node = node.next print("None") # Construct s1 and s2 out of given linked list with consecutive # elements of the list in forward and backward direction respectively def construct(head, s1, s2): if head is None: # Base case return s1 += head.data construct(head.next, s1, s2) s2 += str(reversed(head.data)) # Function to check if a given linked list of Strings is palindromic def isPalindromic(head): # construct s1 with consecutive elements of the linked list # construct s2 by reversing consecutive elements of the linked list # starting from the end s1 = s2 = "" construct(head, s1, s2) # check if linked list is palindromic return s1 == s2 if __name__ == '__main__': head = Node("AA") head.next = Node("XYZ") head.next.next = Node("CD") head.next.next.next = Node("C") head.next.next.next.next = Node("ZYX") head.next.next.next.next.next = Node("AA") printList(head) if isPalindromic(head): print("Palindrome") else: print("Not a Palindrome") 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 # Data Structure to store a linked list node class Node : def __init__ ( self , str ) : self . data = str self . next = None # Function to print a linked list def printList ( node ) : while node : print ( node . data , end = " -> " ) node = node . next print ( "None" ) # Construct s1 and s2 out of given linked list with consecutive # elements of the list in forward and backward direction respectively def construct ( head , s1 , s2 ) : if head is None : # Base case return s1 += head . data construct ( head . next , s1 , s2 ) s2 += str ( reversed ( head . data ) ) # Function to check if a given linked list of Strings is palindromic def isPalindromic ( head ) : # construct s1 with consecutive elements of the linked list # construct s2 by reversing consecutive elements of the linked list # starting from the end s1 = s2 = "" construct ( head , s1 , s2 ) # check if linked list is palindromic return s1 == s2 if __name__ == '__main__' : head = Node ( "AA" ) head . next = Node ( "XYZ" ) head . next . next = Node ( "CD" ) head . next . next . next = Node ( "C" ) head . next . next . next . next = Node ( "ZYX" ) head . next . next . next . next . next = Node ( "AA" ) printList ( head ) if isPalindromic ( head ) : print ( "Palindrome" ) else : print ( "Not a Palindrome" ) Download Run Code Output:



AA -> XYZ -> CD -> C -> ZYX -> AA -> None

Palindrome













(10 votes, average: 5.00 out of 5)

Loading...

Thanks for reading.