@Test(expected = IllegalArgumentException.class) public void shouldNotAcceptNullListAsInputIterative() { palindromeList.checkIterative(null); }
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 { 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 { 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(); }
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); }
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);  }