@Test public void testIsPalindromeVariant() { LinkedList<Character> linkedList = new LinkedList<Character>(); linkedList.add('c'); linkedList.add('i'); linkedList.add('v'); linkedList.add('i'); linkedList.add('c'); assertTrue(isPalindromeVariant(linkedList)); linkedList = new LinkedList<Character>(); linkedList.add('a'); linkedList.add('n'); linkedList.add('n'); linkedList.add('a'); assertTrue(isPalindromeVariant(linkedList)); linkedList = new LinkedList<Character>(); linkedList.add('a'); linkedList.add('n'); linkedList.add('m'); linkedList.add('a'); assertFalse(isPalindromeVariant(linkedList)); }
public static <E> boolean isPalindromeVariant(LinkedList<E> linkedList) { LinkedListNode<E> slow = linkedList.head().successor(); LinkedListNode<E> fast = slow; Stack<E> stack = new Stack<E>(); while (fast != linkedList.tail() && fast.successor() != linkedList.tail()) { stack.push(slow.data()); slow = slow.successor(); fast = fast.successor().successor(); } if (fast == linkedList.tail() && !slow.data().equals(stack.pop())) { return false; } do { slow = slow.successor(); if (!slow.data().equals(stack.pop())) { return false; } } while (slow.successor() != linkedList.tail()); return true; }
PracticeQuestionsCh2 { public static <E> boolean isPalindromeVariant(LinkedList<E> linkedList) { LinkedListNode<E> slow = linkedList.head().successor(); LinkedListNode<E> fast = slow; Stack<E> stack = new Stack<E>(); while (fast != linkedList.tail() && fast.successor() != linkedList.tail()) { stack.push(slow.data()); slow = slow.successor(); fast = fast.successor().successor(); } if (fast == linkedList.tail() && !slow.data().equals(stack.pop())) { return false; } do { slow = slow.successor(); if (!slow.data().equals(stack.pop())) { return false; } } while (slow.successor() != linkedList.tail()); return true; } }
PracticeQuestionsCh2 { public static <E> boolean isPalindromeVariant(LinkedList<E> linkedList) { LinkedListNode<E> slow = linkedList.head().successor(); LinkedListNode<E> fast = slow; Stack<E> stack = new Stack<E>(); while (fast != linkedList.tail() && fast.successor() != linkedList.tail()) { stack.push(slow.data()); slow = slow.successor(); fast = fast.successor().successor(); } if (fast == linkedList.tail() && !slow.data().equals(stack.pop())) { return false; } do { slow = slow.successor(); if (!slow.data().equals(stack.pop())) { return false; } } while (slow.successor() != linkedList.tail()); return true; }  }
PracticeQuestionsCh2 { public static <E> boolean isPalindromeVariant(LinkedList<E> linkedList) { LinkedListNode<E> slow = linkedList.head().successor(); LinkedListNode<E> fast = slow; Stack<E> stack = new Stack<E>(); while (fast != linkedList.tail() && fast.successor() != linkedList.tail()) { stack.push(slow.data()); slow = slow.successor(); fast = fast.successor().successor(); } if (fast == linkedList.tail() && !slow.data().equals(stack.pop())) { return false; } do { slow = slow.successor(); if (!slow.data().equals(stack.pop())) { return false; } } while (slow.successor() != linkedList.tail()); return true; }  static void removeDupes(LinkedList<E> linkedList); static void partition(LinkedList<E> linkedList, E pivot); static boolean isPalindrome(LinkedList<E> linkedList); static boolean isPalindromeVariant(LinkedList<E> linkedList); }
PracticeQuestionsCh2 { public static <E> boolean isPalindromeVariant(LinkedList<E> linkedList) { LinkedListNode<E> slow = linkedList.head().successor(); LinkedListNode<E> fast = slow; Stack<E> stack = new Stack<E>(); while (fast != linkedList.tail() && fast.successor() != linkedList.tail()) { stack.push(slow.data()); slow = slow.successor(); fast = fast.successor().successor(); } if (fast == linkedList.tail() && !slow.data().equals(stack.pop())) { return false; } do { slow = slow.successor(); if (!slow.data().equals(stack.pop())) { return false; } } while (slow.successor() != linkedList.tail()); return true; }  static void removeDupes(LinkedList<E> linkedList); static void partition(LinkedList<E> linkedList, E pivot); static boolean isPalindrome(LinkedList<E> linkedList); static boolean isPalindromeVariant(LinkedList<E> linkedList);  }