PalindromeList { public boolean checkIterative(ListNode list) { validateInput(list); Stack<ListNode> stack = new Stack<ListNode>(); ListNode fastPointer = list; ListNode slowPointer = list; while (fastPointer != null && fastPointer.getNext() != null) { stack.add(slowPointer); slowPointer = slowPointer.getNext(); fastPointer = fastPointer.getNext().getNext(); } if (fastPointer != null) { slowPointer = slowPointer.getNext(); } boolean isPalindrome = true; while (slowPointer != null) { if (!stack.pop().equals(slowPointer)) { isPalindrome = false; break; } slowPointer = slowPointer.getNext(); } return isPalindrome; } PalindromeList(); boolean checkReversing(ListNode list); boolean checkIterative(ListNode list);  }
@Test(expected = IllegalArgumentException.class) public void shouldNotAcceptNullListAsInputIterative() { palindromeList.checkIterative(null); }
@Test public void shouldReturnTrueIfListContainsJustOneNodeIterative() { ListNode<Integer> list = new ListNode<Integer>(1); boolean result = palindromeList.checkIterative(list); assertTrue(result); }
@Test public void shouldRturnTrueIfListContainsTwoElementsWithTheSameValueIterative() { ListNode<Integer> list = createList(new int[] { 1, 1 }); boolean result = palindromeList.checkIterative(list); assertTrue(result); }
@Test public void shouldReturnTrueIfListIsPalindromeIterative() { ListNode<Integer> list = createList(new int[] { 1, 2, 1 }); boolean result = palindromeList.checkIterative(list); assertTrue(result); }
@Test public void shouldReturnFalseIfListIsNotPalindromeIterative() { ListNode<Integer> list = createList(new int[] { 1, 1, 3 }); boolean result = palindromeList.checkIterative(list); assertFalse(result); }