Skip to content
Related Articles

Related Articles

Java 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. 


// 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 =;
        // 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"); = new Node("bc"); = new Node("d"); =
             new Node("dcb"); =
             new Node("a");
// This code is contributed by Amit Khandelwal



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!