输入一个链表,输出该链表中倒数第k个结点。
1 # -*- coding:utf-8 -*- 2 # class ListNode: 3 # def __init__(self, x): 4 # self.val = x 5 # self.next = None 6 7 class Solution: 8 def FindKthToTail(self, head, k): 9 fast = ListNode(0)10 slow = ListNode(0)11 if head == None:12 return None13 safehead = ListNode(head.val)14 safehead.next = head.next15 fast = head16 count = 017 while count != k:18 if fast == None:19 return None20 fast = fast.next21 count += 122 slow = safehead23 while fast != None:24 fast = fast.next25 slow = slow.next26 return slow