@@ -12,29 +12,63 @@ public class Palindrome {
12
12
13
13
/**
14
14
* Checks whether a Linked List is palindrome by using a stack.
15
- *
15
+ *
16
16
* @param head starting node of the linked list.
17
17
* @return {@code true} if linked list palindrome, {@code false} otherwise.
18
18
*/
19
19
private static boolean isPalindrome (Node head ) {
20
20
Node curr = head ;
21
21
Stack <Integer > stack = new Stack <>();
22
+ // pop all elements into stack
22
23
while (curr != null ) {
23
24
stack .push (curr .val );
24
25
curr = curr .next ;
25
26
}
26
27
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 ()) {
29
31
return false ;
30
32
}
31
33
curr = curr .next ;
32
34
}
33
35
return true ;
34
36
}
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
+
36
70
public static void main (String [] args ) {
37
-
71
+
38
72
Node l1 = new Node (1 );
39
73
l1 .next = new Node (2 );
40
74
l1 .next .next = new Node (3 );
@@ -43,6 +77,7 @@ public static void main(String[] args) {
43
77
l1 .next .next .next .next .next = new Node (1 );
44
78
printList (l1 );
45
79
System .out .println (isPalindrome (l1 ));
80
+ System .out .println (isPalindromeOptimized (l1 ));
46
81
System .out .println ("------" );
47
82
48
83
l1 = new Node (1 );
@@ -52,6 +87,7 @@ public static void main(String[] args) {
52
87
l1 .next .next .next .next = new Node (1 );
53
88
printList (l1 );
54
89
System .out .println (isPalindrome (l1 ));
90
+ System .out .println (isPalindromeOptimized (l1 ));
55
91
System .out .println ("------" );
56
92
57
93
l1 = new Node (0 );
@@ -61,10 +97,13 @@ public static void main(String[] args) {
61
97
l1 .next .next .next .next = new Node (0 );
62
98
printList (l1 );
63
99
System .out .println (isPalindrome (l1 ));
100
+ System .out .println (isPalindromeOptimized (l1 ));
64
101
System .out .println ("------" );
65
-
102
+
66
103
l1 = new Node (1 );
67
104
printList (l1 );
68
105
System .out .println (isPalindrome (l1 ));
106
+ System .out .println (isPalindromeOptimized (l1 ));
107
+
69
108
}
70
109
}
0 commit comments