Check if linked list of string forms a pallindrome

Problem: For a given linked list containing string, check if it forms a pallindrome.

Problem Explanation: A pallindrome is a word, phrase, number or other sequence of characters which reads the same backward or forward. Example: civic, level etc. For a given linked list N -> IT -> IN,  NITIN  forms a pallindrome. Hence output is true.

Java Implementation

public class PallindromeLL {
    public boolean isPallindrome(Node node) {
        String string = "";
        while (node != null) {
            string = string + node.getValue();
            node = node.getLink();
        }

        int strLength = string.length();
        if (strLength == 0) {
            return false;
        }

        for (int i = 0; i < strLength; i++) {
            if (string.charAt(i) != string.charAt(strLength - 1 - i)) {
                return false;
            }
        }
        return true;
    }

    public static void main(String[] args) {
        Node node1 = new Node("N", null);
        node1.setLink(new Node("IT", null));
        node1.getLink().setLink(new Node("IN", null));

        PallindromeLL pallindromeLL = new PallindromeLL();
        System.out.println(pallindromeLL.isPallindrome(node1));
    }
}

public class Node {
    private String value;
    private Node link;

    public Node(String value, Node n) {
        this.value = value;
        this.link = n;
    }

    public String getValue() {
        return value;
    }

    public void setValue(String value) {
        this.value = value;
    }

    public Node getLink() {
        return link;
    }

    public void setLink(Node link) {
        this.link = link;
    }
}

TimeComplexity: O(n)


Note: If you find any other better way to approach to this problem or you find any issue/error in above code snippets/approaches – please share it in the comments section below and get a chance to enrol free of cost for an online Live Data Structure and Algorithm course (specially designed for interview preparation for software companies like Amazon, Google, Facebook, FlipKart, SnapDeal, HealthKart…)