这个题不需要考虑返回的node后面还跟着其他node。
/*
public class ListNode {
int val;
ListNode next = null;
ListNode(int val) {
this.val = val;
}
}*/
public class Solution {
public ListNode FindKthToTail(ListNode head,int k) {
if(head==null){
return null;
}
ListNode fast = head;
ListNode slow = head;
//如果k还没有循环完,fast就走到了最后,说明k大于链表长度
for(int i=1;i<=k;i++){
if(fast==null){
return null;
}
fast=fast.next;
}
while(fast!=null){
fast = fast.next;
slow = slow.next;
}
return slow;
}
}