Find Kth from Tail
by Isai Damier

#=======================================================================
# Author: Isai Damier
# Title: Singly Linked List
# Project: geekviewpoint
# Package: datastructure
#
# Description: A LinkedList is a data structure that allows access
#   to a collection of data using pointers/references. While an
#   array can also be defined as above, LinkedLists and arrays differ
#   in how they are stored in memory and in the operations they
#   allow. Unlike an array that must be stored in a block of memory,
#   the nodes of a LinkedList can be stored anywhere because each
#   node has a reference to the node that succeeds it. Because the
#   nodes are stored so loosely, inserting nodes into a LinkedList
#   is easy; whereas in an array, all the succeeding elements must
#   be shifted. Of course, insertion also means changing the size of
#   the array, which means creating the entire array anew.
#
#   Perhaps the greatest beauty of LinkedList is that it allows
#   accessing an entire sequence of nodes using only one variable:
#   a reference to the first node in the sequence.
#
#   Countless operations can be performed on LinkedLists. Following
#   are a few, ranging from the common to the very interesting.
#=======================================================================
  #=====================================================================
  # Statement:
  #   Retrieve the node that is k units from the tail.
  #
  # Time Complexity of Solution:
  #   Best = Worst = O(n).
  #
  # Technical Details: The imagery is to get a poll, k units long.
  #   Place the poll along the LinkedList so that one end
  #   (labeled X) of the poll is by the self.head node and one end
  #   (labeled Y) is by some other node called tmp. Now slide the
  #   poll along the LinkedList, until the end labeled Y reaches
  #   the self.tail. At this point, return the node by X.
  #=====================================================================
 
 import collections
class SinglyLinkedList( object ):

  def __init__( self ):
    self.head , self.tail = None, None

  def findKthFromTail( self, k ):
    if 0 > k:
      return None

    # count k units from the self.head.
    tmp = self.head
    count = 0
    while count < k and None != tmp:
      tmp = tmp.next
      count += 1

    # if the LinkedList does not contain k elements, return None
    if count < k or None == tmp:
      return None

    # keeping tab on the kth element from tmp, slide tmp until
    # tmp equals self.tail. Then return the kth element.
    kth = self.head
    while None != tmp.next:
      tmp = tmp.next
      kth = kth.next

    return kth


class Node( object ):

  def __init__( self, data, next = None ):
    self.data = data
    self.next = next
import unittest
from algorithms.SinglyLinkedList import SinglyLinkedList
import random

class Test( unittest.TestCase ):
  #=====================================================================
  # Test of findKthFromTail method, of class SinglyLinkedList.
  #=====================================================================
  def testFindKthFromTail( self ):
    tape = [9, 4, 5, 2, 1, 12, 6, 7, 4, 8, 3, 0, 16, 19, 11]
    linkedList = SinglyLinkedList()
    for i in range( len( tape ) ):
      linkedList.addToTail( tape[i] )

    k = 0
    last = len( tape ) - 1
    self.assertEquals( tape[last - k], linkedList.findKthFromTail( k ).data )
    k = last / 2
    self.assertEquals( tape[last - k], linkedList.findKthFromTail( k ).data )
    k = last
    self.assertEquals( tape[last - k], linkedList.findKthFromTail( k ).data )
    k = last + 21
    self.assertEquals( None, linkedList.findKthFromTail( k ) )
    k = -1
    self.assertEquals( None, linkedList.findKthFromTail( k ) )