@@ -12,29 +12,63 @@ public class Palindrome {
1212
1313    /** 
1414     * Checks whether a Linked List is palindrome by using a stack. 
15-      *   
15+      * 
1616     * @param head starting node of the linked list. 
1717     * @return {@code true} if linked list palindrome, {@code false} otherwise. 
1818     */ 
1919    private  static  boolean  isPalindrome (Node  head ) {
2020        Node  curr  = head ;
2121        Stack <Integer > stack  = new  Stack <>();
22+         // pop all elements into stack 
2223        while  (curr  != null ) {
2324            stack .push (curr .val );
2425            curr  = curr .next ;
2526        }
2627        curr  = head ;
27-         while  (!stack .empty () && curr  != null ) {
28-             if  (stack .pop () != curr .val ) {
28+         // as stack contains the elements in reverse order, pop and compare with the list one by one 
29+         while  (curr  != null ) {
30+             if  (curr .val  != stack .pop ()) {
2931                return  false ;
3032            }
3133            curr  = curr .next ;
3234        }
3335        return  true ;
3436    }
35-     
37+ 
38+     /** 
39+      * This is a similar approach like above but a bit faster as we are not iterating the entire list twice. 
40+      * 
41+      * @param head starting node of the linked list. 
42+      * @return {@code true} if linked list palindrome, {@code false} otherwise. 
43+      */ 
44+     private  static  boolean  isPalindromeOptimized (Node  head ) {
45+         Node  slow  = head ;
46+         Node  fast  = head ;
47+         Stack <Integer > stack  = new  Stack <>();
48+ 
49+         // push half of the elements into the stack 
50+         while  (fast  != null  && fast .next  != null ) {
51+             stack .push (slow .val );
52+             slow  = slow .next ;
53+             fast  = fast .next .next ;
54+         }
55+ 
56+         // linked list has odd number of elements, so forward the slow reference by one 
57+         if  (fast  != null ) {
58+             slow  = slow .next ;
59+         }
60+ 
61+         while  (slow  != null ) {
62+             if  (slow .val  != stack .pop ()) {
63+                 return  false ;
64+             }
65+             slow  = slow .next ;
66+         }
67+         return  true ;
68+     }
69+ 
3670    public  static  void  main (String [] args ) {
37-          
71+ 
3872        Node  l1  = new  Node (1 );
3973        l1 .next  = new  Node (2 );
4074        l1 .next .next  = new  Node (3 );
@@ -43,6 +77,7 @@ public static void main(String[] args) {
4377        l1 .next .next .next .next .next  = new  Node (1 );
4478        printList (l1 );
4579        System .out .println (isPalindrome (l1 ));
80+         System .out .println (isPalindromeOptimized (l1 ));
4681        System .out .println ("------" );
4782
4883        l1  = new  Node (1 );
@@ -52,6 +87,7 @@ public static void main(String[] args) {
5287        l1 .next .next .next .next  = new  Node (1 );
5388        printList (l1 );
5489        System .out .println (isPalindrome (l1 ));
90+         System .out .println (isPalindromeOptimized (l1 ));
5591        System .out .println ("------" );
5692
5793        l1  = new  Node (0 );
@@ -61,10 +97,13 @@ public static void main(String[] args) {
6197        l1 .next .next .next .next  = new  Node (0 );
6298        printList (l1 );
6399        System .out .println (isPalindrome (l1 ));
100+         System .out .println (isPalindromeOptimized (l1 ));
64101        System .out .println ("------" );
65-          
102+ 
66103        l1  = new  Node (1 );
67104        printList (l1 );
68105        System .out .println (isPalindrome (l1 ));
106+         System .out .println (isPalindromeOptimized (l1 ));
107+ 
69108    }
70109}
0 commit comments