输入一个链表,输出该链表中倒数第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 None 13 safehead = ListNode(head.val) 14 safehead.next = head.next 15 fast = head 16 count = 0 17 while count != k: 18 if fast == None: 19 return None 20 fast = fast.next 21 count += 1 22 slow = safehead 23 while fast != None: 24 fast = fast.next 25 slow = slow.next 26 return slow