Java Program To Check If A Linked List Of Strings Forms A Palindrome
Last Updated :
22 Jun, 2022
Improve
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.
// Java Program to check if a given linked list
// of strings form a palindrome
import java.util.Scanner;
// Linked List node
class Node
{
String data;
Node next;
Node(String d)
{
data = d;
next = null;
}
}
class LinkedList_Palindrome
{
Node head;
// 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()
{
Node node = head;
// 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 code
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());
}
}
// This code is contributed by Amit Khandelwal
// Java Program to check if a given linked list
// of strings form a palindrome
import java.util.Scanner;
// Linked List node
class Node
{
String data;
Node next;
Node(String d)
{
data = d;
next = null;
}
}
class LinkedList_Palindrome
{
Node head;
// 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()
{
Node node = head;
// 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 code
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());
}
}
// This code is contributed by Amit Khandelwal
Output:
true
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!