题目:
输入一个链表,输出该链表中倒数第k个结点。
python实现:找一个空列表存链表的所有值,便于查找想要的节点。
#-*- coding:utf-8 -*-
#class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution:
def FindKthToTail(self, head, k):
# write code here
le=0
l=[] #找一个空列表将链表的值转存进去,便于查找和输出节点
while head!=None:
le+=1 #链表长度
l.append(head)
head=head.next
if k<=le and k>=1:
return l[-k]
else:
return None