题目描述:输入一个链表,输出该链表中倒数第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
#首先遍历链表,得到链表长度
count = 0
cur = head
while cur:
count += 1
cur = cur.next
#倒数第K个节点就是顺序第count-k+1个节点
i = 1
#k不能大于链表长度
if k <= count:
for i in range(1,count-k+1):
head =head.next
return head
else:
return None