FindKthElement { public ListNode find3(ListNode listNode, int k) { validateInput(listNode, k); ListNode pointer2 = listNode; for (int i = 0; i <= k; i++) { if (pointer2 == null) { throw new IndexOutOfBoundsException(); } pointer2 = pointer2.getNext(); } ListNode result = listNode; while (pointer2 != null) { result = result.getNext(); pointer2 = pointer2.getNext(); } return result; } FindKthElement(); ListNode find(ListNode listNode, int k); ListNode find2(ListNode listNode, int k); ListNode find3(ListNode listNode, int k);  }
@Test(expected = IllegalArgumentException.class) public void shouldNotAcceptNullListNodes3() { findElement.find3(null, 4); }
@Test(expected = IllegalArgumentException.class) public void shouldNotAcceptNegativePositions3() { findElement.find3(new ListNode<Integer>(3), -1); }
@Test(expected = IndexOutOfBoundsException.class) public void shouldNotAcceptPositionsGreaterThanListSize3() { findElement.find3(new ListNode<Integer>(3), 2); }
@Test public void shouldReturnLastNodeIfPositionIsZero3() { ListNode result = findElement.find3(new ListNode<Integer>(1), 0); ListNode<Integer> expectedNode = new ListNode<Integer>(1); assertEquals(expectedNode, result); }
@Test public void shouldReturnFirstElementIfPositionIsEqualsToListSizeMinusOne3() { ListNode<Integer> list = createList(new int[] { 1, 2, 3 }); ListNode result = findElement.find3(list, 2); ListNode<Integer> expectedNode = new ListNode<Integer>(1); assertEquals(expectedNode, result); }
@Test public void shouldReturnKthToLastElement3() { ListNode<Integer> list = createList(new int[] { 1, 2, 3 }); ListNode result = findElement.find3(list, 1); ListNode<Integer> expectedNode = new ListNode<Integer>(2); assertEquals(expectedNode, result); }