GetTheElementInTheMiddleOfTheList { public ListNode<Integer> get2(ListNode<Integer> list) { validateInput(list); ListNode<Integer> currentNode = list; ListNode<Integer> fastNode = list.getNext(); while (fastNode != null && fastNode.getNext() != null) { currentNode = currentNode.getNext(); fastNode = fastNode.getNext().getNext(); } return currentNode; }  ListNode<Integer> get(ListNode<Integer> list); ListNode<Integer> get2(ListNode<Integer> list);  }
@Test(expected = IllegalArgumentException.class) public void shouldNotAcceptNullListInstances2() { getTheElementInTheMiddleOfTheList.get2(null); }
@Test public void shouldReturnTheElementInTheMiddleOfAListWithAnEvenNumberOfElements2() { ListNode<Integer> list = createList(new int[] { 1, 2, 3 }); ListNode<Integer> middleElement = getTheElementInTheMiddleOfTheList.get2(list); assertEquals(Integer.valueOf(2), middleElement.getData()); }
@Test public void shouldReturnTheElementInTheMiddleOfTheListWithAnOddNumberOfElements2() { ListNode<Integer> list = createList(new int[] { 1, 2, 3, 4 }); ListNode<Integer> middleElement = getTheElementInTheMiddleOfTheList.get2(list); assertEquals(Integer.valueOf(2), middleElement.getData()); }