输入一个链表,输出该链表中倒数第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
firstpoint = head
secondpoint = head
for i in range(k):
if firstpoint == None: #注意边界值的判断,判断条件的位置需要在遇到结点之前
return None
firstpoint = firstpoint.next
while firstpoint:
firstpoint = firstpoint.next
secondpoint = secondpoint.next
return secondpoint
注意边界值的情况,例如链表的总长度为3,返回倒数第3个结点