/*
public class ListNode
int val;
ListNode next = null;
ListNode(int val) {
this.val = val;
}
}*/
public class Solution {
public ListNode FindKthToTail(ListNode head,int k) {
ListNode fast = head;
ListNode slow = head;
if(head == null||k==0) {
return null;
}
for( int i = 0;i<k-1;i++) {
if(fast.next!=null) {
fast = fast.next;
}else {
return null; //如果在k-1步之内,fast的下一个节点为空了,说明数组长度不够,k值取大了,容易越界
}
}
while(fast.next!=null) {
fast = fast.next;
slow = slow.next;
}
return slow;
}
}