题目描述
输入一个链表,输出该链表中倒数第k个结点。
/**
* @author Joeson Chan
*/
public class Solution {
public ListNode FindKthToTail(ListNode head,int k) {
if(null == head || k < 0){
return null;
}
ListNode pre = head;
ListNode post = head;
while(null != pre && null != post){
if(k-- <= 0){
post = post.next;
}
pre = pre.next;
}
return k > 0 ? null : post;
}
}