package com.interview.linklist;

/**
 * http://www.geeksforgeeks.org/function-to-check-if-a-singly-linked-list-is-palindrome/ 
 * Test cases:
 * odd number of nodes
 * even number of nodes
 * 0 1 or more nodes
 * palindrome list
 * non palindrom list
 */
public class LinkListIsPalindrome {

	public boolean isPalindrome(NodeRef head,Node end){
		if(end == null){
			return true;
		}
		boolean r = isPalindrome(head,end.next);
		r = r && head.node.data == end.data;
		head.next();
		return r;
	}
	
	public static void main(String args[]){
		LinkList ll = new LinkList();
		Node head = null;
		head = ll.addNode(1, head);
		head = ll.addNode(2, head);
		head = ll.addNode(3, head);
		head = ll.addNode(4, head);
		head = ll.addNode(3, head);
		head = ll.addNode(2, head);
		head = ll.addNode(1, head);
		NodeRef nodeRef = new NodeRef();
		nodeRef.node = head;
		LinkListIsPalindrome llp = new LinkListIsPalindrome();
		System.out.println(llp.isPalindrome(nodeRef, head));
	}
}

@@ -285,17 +285,20 @@
 rome %7B%0A%0A
-%09
+    
 public b
@@ -341,18 +341,24 @@
 e end)%7B%0A
-%09%09
+        
 if(end =
@@ -370,11 +370,20 @@
 l)%7B%0A
-%09%09%09
+            
 retu
@@ -395,14 +395,26 @@
 ue;%0A
-%09%09%7D%0A%09%09
+        %7D%0A        
 bool
@@ -450,18 +450,24 @@
 .next);%0A
-%09%09
+        
 r = r &&
@@ -495,18 +495,24 @@
 d.data;%0A
-%09%09
+        
 head.nex
@@ -516,18 +516,24 @@
 next();%0A
-%09%09
+        
 return r
@@ -538,14 +538,23 @@
  r;%0A
-%09%7D%0A%09%0A%09
+    %7D%0A    %0A    
 publ
@@ -589,18 +589,24 @@
 rgs%5B%5D)%7B%0A
-%09%09
+        
 LinkList
@@ -627,18 +627,24 @@
 List();%0A
-%09%09
+        
 Node hea
@@ -653,18 +653,24 @@
 = null;%0A
-%09%09
+        
 head = l
@@ -681,34 +681,40 @@
 dNode(1, head);%0A
-%09%09
+        
 head = ll.addNod
@@ -717,34 +717,40 @@
 dNode(2, head);%0A
-%09%09
+        
 head = ll.addNod
@@ -753,34 +753,40 @@
 dNode(3, head);%0A
-%09%09
+        
 head = ll.addNod
@@ -789,34 +789,40 @@
 dNode(4, head);%0A
-%09%09
+        
 head = ll.addNod
@@ -825,34 +825,40 @@
 dNode(3, head);%0A
-%09%09
+        
 head = ll.addNod
@@ -869,18 +869,24 @@
  head);%0A
-%09%09
+        
 head = l
@@ -905,18 +905,24 @@
  head);%0A
-%09%09
+        
 NodeRef 
@@ -946,18 +946,24 @@
 eRef();%0A
-%09%09
+        
 nodeRef.
@@ -975,18 +975,24 @@
 = head;%0A
-%09%09
+        
 LinkList
@@ -1042,10 +1042,16 @@
 ();%0A
-%09%09
+        
 Syst
@@ -1103,9 +1103,12 @@
 ));%0A
-%09
+    
 %7D%0A%7D%0A

Change tabs to space