/*
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 || k <= 0)
return null;
ListNode temp = head;
while((k > 1) && (temp != null)) { //先让第一个指针先走(k-1)步
temp = temp.next;
k--;
}
if((temp == null)) //如果 k大于链表的长度,返回空
return temp;
ListNode result = head;
while(temp.next != null) { //两个指针一起走,temp走到末尾指针时,result也就走到第(k-1)也就是倒数第k个结点了。
result = result.next;
temp = temp.next;
}
return result;
}
}