/*
public class ListNode {
int val;
ListNode next = null;
ListNode(int val) {
this.val = val;
}
}*/
public class Solution {
public ListNode FindKthToTail(ListNode head,int k) {
//5-->2-->3-->4-->1--7>-->9-->null
//双指针1号,2号指向头节点,其中1号先走k个节点然后两个一起遍历,如果nowNOde1-->null,nowNOde2 = node(k);
if (head == null || k <= 0) { // 链表不能为空,查找的倒数第k个结点k不能小于0
return null;
}
int i = 0;
ListNode nowNOde1 = head;
ListNode nowNOde2 = head;
while(i<k-1){
if(nowNOde1.next!=null){
nowNOde1 = nowNOde1.next;
i++;
}else{
return null;
}
}
while(nowNOde1.next!=null){
nowNOde1 = nowNOde1.next;
nowNOde2 = nowNOde2.next;
}
return nowNOde2;
}
}