# -*- 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
d=[]
if not head:
return None
count=0
while head:
d.append(head)
head=head.next
count+=1
if k>count or k<1:
return
return d[-k]
两个指针,一个指针先走k-1步,在一起走。类似于一个长k的尺子,右侧到头了,左侧就是要输出的值
if not head or k<=0:
return None
slow=fast=head
while k-1>0:
if slow.next!=None:
k-=1
slow=slow.next
else:
return None
while slow.next:
slow=slow.next
fast=fast.next
return fast