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