Ejemplo n.º 1
0
        // return node at kth position from end.
        public LinkedListNode FindKthFromEnd(LinkedListNode head, int k, NodeWraperKthELe indx)
        {
            if (head == null)
            {
                return(null);
            }

            var node = FindKthFromEnd(head.Next, k, indx);

            indx.index = indx.index + 1;

            if (indx.index == k)
            {
                return(head);
            }

            return(node);
        }
Ejemplo n.º 2
0
        /*
         * Recursive solution:
         * Traverse through end of list
         * return int from each recursive call
         * last element returns 1
         * then each returns +1 to it.
         * when retrun value reach k keep returning k
         */

        public int Test1()
        {
            LinkedListNode head = new LinkedListNode(1);

            head.Next                          = new LinkedListNode(2);
            head.Next.Next                     = new LinkedListNode(3);
            head.Next.Next.Next                = new LinkedListNode(1);
            head.Next.Next.Next.Next           = new LinkedListNode(4);
            head.Next.Next.Next.Next.Next      = new LinkedListNode(5);
            head.Next.Next.Next.Next.Next.Next = new LinkedListNode(6);

            var kThElement = FindKthElementIterative(head, 3);


            NodeWraperKthELe indx = new NodeWraperKthELe();

            var kthNOde = FindKthFromEnd(head, 3, indx);


            return(FindKthFromEnd(head, 3));
        }