Title: Enter a linked list, the linked list output penultimate node k.
def find_Kth_tail(head,k): if not head or k<1: return None p = head q = head cnt = 0 while p: cnt+=1 p=p.next if k>cnt: return None p = head for i in range(k): p = p.next while p: p = p.next q = q.next return q.data
Note:
Use two pointers, a pointer k go first step, then two hands while walking, a current list pointer reaches the end of time, after a pointer is the penultimate k nodes. This interpretation of the data input to be noted that the question of whether the first node is empty, if k is greater than 0, and k is smaller than the total length of the list.