(head, k)
return p2
This is an optimal method using iteration.
    We move p1 k steps ahead into the list.
    Then we move p1 and p2 together until p1 hits the end.
This is an optimal method using iteration.
    We move p1 k steps ahead into the list.
    Then we move p1 and p2 together until p1 hits the end.
This is an optimal method using iteration . We move p1 k steps ahead into the list . Then we move p1 and p2 together until p1 hits the end .
def kth_to_last(head, k):
    """
    This is an optimal method using iteration.
    We move p1 k steps ahead into the list.
    Then we move p1 and p2 together until p1 hits the end.
    """
    if not (head or k > -1):
        return False
    p1 = head
    p2 = head
    for i in range(1, k + 1):
        if p1 is None:
            # Went too far, k is not valid
            raise IndexError
        p1 = p1.next
    while p1:
        p1 = p1.next
        p2 = p2.next
    return p2
def kth_to_last ( head , k ) : if not ( head or k > - 1 ) : return False p1 = head p2 = head for i in range ( 1 , k + 1 ) : if p1 is None : # Went too far, k is not valid raise IndexError p1 = p1 . next while p1 : p1 = p1 . next p2 = p2 . next return p2