MCPcopy Index your code
hub / github.com/careercup/ctci / isPalindrome

Method isPalindrome

java/Chapter 2/Question2_7/QuestionB.java:8–34  ·  view source on GitHub ↗
(LinkedListNode head)

Source from the content-addressed store, hash-verified

6
7public class QuestionB {
8 public static boolean isPalindrome(LinkedListNode head) {
9 LinkedListNode fast = head;
10 LinkedListNode slow = head;
11
12 Stack<Integer> stack = new Stack<Integer>();
13
14 while (fast != null && fast.next != null) {
15 stack.push(slow.data);
16 slow = slow.next;
17 fast = fast.next.next;
18 }
19
20 /* Has odd number of elements, so skip the middle */
21 if (fast != null) {
22 slow = slow.next;
23 }
24
25 while (slow != null) {
26 int top = stack.pop().intValue();
27 System.out.println(slow.data + " " + top);
28 if (top != slow.data) {
29 return false;
30 }
31 slow = slow.next;
32 }
33 return true;
34 }
35
36 public static void main(String[] args) {
37 int length = 9;

Callers 1

mainMethod · 0.95

Calls 2

pushMethod · 0.95
popMethod · 0.95

Tested by

no test coverage detected