//method 2, recursion. O(n) time, O(n) space, start with counter = 0 static LinkListNode kthToLastEleRecursive(LinkListNode head, int k, Index idx) { if (head == null) { return null; } LinkListNode node = kthToLastEleRecursive(head.next, k, idx); idx.value += 1; if (idx.value == k) { return head; } return node; }
static LinkListNode kthToLastEleRecursive(LinkListNode head, int k) { Index idx = new Index(); return kthToLastEleRecursive(head, k, idx); }