| 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; |