题目描述:
代码:
class Solution(object):
def getKthFromEnd(self, head, k):
index_head,index_tail=head,head
#如果链表为None或者K为负值,则返回None
if head == None or k<=0:
return None
for i in range(k):
#如果在K的范围内,链表为空,表明K值大于链表长度,依旧返回为None
if index_head==None:
return None
index_head=index_head.next
#双指针前进,当先行的头指针到达链表末尾时,尾指针正在K位置
while index_head:
index_head=index_head.next
index_tail=index_tail.next
return index_tail