问题:
输入一个链表,输出该链表中倒数第k个结点。
代码:
# -*- 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
res=[]
n=0
while head:
res.append(head)
head=head.next
n+=1
if k==0 or n<k:
return None
else:
return res[-k]
运行时间:26ms
占用内存:5860k