Check if a linked list of strings forms a palindrome | Java Program to check if a given linked list of strings form a palindrome ; Linked List node ;  ; 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 ; Check if the formed string is palindrome ; Driver program to test above function
import java . util . Scanner ; class Node { String data ; Node next ; Node ( String d ) { data = d ; next = null ; } } class LinkedList_Palindrome { Node head ; boolean isPalidromeUtil ( String str ) { int length = str . length ( ) ; for ( int i = 0 ; i < length / 2 ; i ++ ) if ( str . charAt ( i ) != str . charAt ( length - i - 1 ) ) return false ; return true ; } boolean isPalindrome ( ) { Node node = head ; String str = " " ; while ( node != null ) { str = str . concat ( node . data ) ; node = node . next ; } return isPalidromeUtil ( str ) ; } public static void main ( String [ ] args ) { LinkedList_Palindrome list = new LinkedList_Palindrome ( ) ; list . head = new Node ( " a " ) ; list . head . next = new Node ( " bc " ) ; list . head . next . next = new Node ( " d " ) ; list . head . next . next . next = new Node ( " dcb " ) ; list . head . next . next . next . next = new Node ( " a " ) ; System . out . println ( list . isPalindrome ( ) ) ; } }