MCPcopy Create free account
hub / github.com/careercup/ctci / isPalindromeRecurse

Method isPalindromeRecurse

java/Chapter 2/Question2_7/Question.java:15–31  ·  view source on GitHub ↗
(LinkedListNode head, int length)

Source from the content-addressed store, hash-verified

13 }
14
15 public Question.Result isPalindromeRecurse(LinkedListNode head, int length) {
16 if (head == null || length == 0) {
17 return new Question.Result(null, true);
18 } else if (length == 1) {
19 return new Question.Result(head.next, true);
20 } else if (length == 2) {
21 return new Question.Result(head.next.next, head.data == head.next.data);
22 }
23 Question.Result res = isPalindromeRecurse(head.next, length - 2);
24 if (!res.result || res.node == null) {
25 return res; // Only "result" member is actually used in the call stack.
26 } else {
27 res.result = head.data == res.node.data;
28 res.node = res.node.next;
29 return res;
30 }
31 }
32
33 public boolean isPalindrome(LinkedListNode head) {
34 int size = 0;

Callers 1

isPalindromeMethod · 0.95

Calls

no outgoing calls

Tested by

no test coverage detected