예제 #1
0
        //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;
        }
예제 #2
0
 static LinkListNode kthToLastEleRecursive(LinkListNode head, int k)
 {
     Index idx = new Index();
     return kthToLastEleRecursive(head, k, idx);
 }