定义两个引用fast和slow,先让fast走k-1步,
然后和slow一起每次走一步直到fast到达末尾结点,
此时slow位于倒数第k个结点。
public Node FindKthToTail(Node head,int k) {
if(head == null) {
return null;
}
Node cur = head;
int count = 0;
while (cur!=null) {
count++;
cur = cur.next;
}
if(k>count) {
return null;
}
if(k<=0) {
return null;
}
Node fast = head;
Node slow = head;
for(int i=0;i<k-1;i++) {
if(fast.next != null){
fast = fast.next;
}else {
return null;
}
}
while(fast.next != null) {
fast = fast.next;
slow = slow.next;
}
return slow;
}