class Node {
public int data;
public Node next;
public Node(int data){
this.data=data;
}
return ""+data;
}
}
/**
* To check if Linked list is pallindrome or not
* @author Prateek
*/
class LinkListPallindrome {
public boolean checkPallindrome(Node head){
Node n =isPallindrome(head,head);
if(n==null)
return false;
return true;
}
public Node isPallindrome(Node head , Node curr){
if(curr == null)
return head;
Node rval = isPallindrome(head, curr.next);
if(rval!=null)
if(rval.data == curr.data)
return rval = (rval.next!=null) ? rval.next : rval;
return null ;
}
public static void main
(String[] args
) {
Node head= new Node(1);
head.next= new Node(2);
head.next.next= new Node(3);
head.next.next.next= new Node(2);
head.next.next.next.next= new Node(1);
LinkListPallindrome obj = new LinkListPallindrome();
System.
out.
println(obj.
checkPallindrome(head
)); }
}
CmNsYXNzIE5vZGUgewoKCXB1YmxpYyBpbnQgZGF0YTsKCXB1YmxpYyBOb2RlIG5leHQ7CgkKCXB1YmxpYyBOb2RlKGludCBkYXRhKXsKCQl0aGlzLmRhdGE9ZGF0YTsKCX0KCQoJcHVibGljIFN0cmluZyB0b1N0cmluZygpewoJCXJldHVybiAiIitkYXRhOwoJfQp9Ci8qKgogKiBUbyBjaGVjayBpZiBMaW5rZWQgbGlzdCBpcyBwYWxsaW5kcm9tZSBvciBub3QKICogQGF1dGhvciBQcmF0ZWVrCiAqLwpjbGFzcyBMaW5rTGlzdFBhbGxpbmRyb21lIHsKCQoJcHVibGljIGJvb2xlYW4gY2hlY2tQYWxsaW5kcm9tZShOb2RlIGhlYWQpewoJCU5vZGUgbiA9aXNQYWxsaW5kcm9tZShoZWFkLGhlYWQpOwoJCWlmKG49PW51bGwpCgkJCXJldHVybiBmYWxzZTsKCQlyZXR1cm4gdHJ1ZTsKCX0KCQoJcHVibGljIE5vZGUgaXNQYWxsaW5kcm9tZShOb2RlIGhlYWQgLCBOb2RlIGN1cnIpewoJCQoJCWlmKGN1cnIgPT0gbnVsbCkKCQkJcmV0dXJuIGhlYWQ7CgkJCgkJTm9kZSBydmFsID0gaXNQYWxsaW5kcm9tZSh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