public class Solution { public ListNode FindKthToTail(ListNode head,int k) { if(k<=0){ return null; } if(head==null){ return null; } ListNode fast=head; ListNode slow=head; while(k-1!=0){ fast=fast.next; if(fast==null){ return null; } k--; } while(fast.next!=null){ fast=fast.next; slow=slow.next; } return slow; } }
fast先走k-1步,运用快慢指针,fast走两步,然后fast和slow同时一步一步走,当fast.next==null,退出循环