题解 | #8.链表中倒数最后k个结点#
链表中倒数最后k个结点
http://www.nowcoder.com/practice/886370fe658f41b498d40fb34ae76ff9
原来这道题也可以快慢指针,惊呆了
注意:1.快指针走k步的时候注意判断fast是否为null
function FindKthToTail( pHead , k ) {
let fast = pHead, slow = pHead;
for(let i=0;i<k;i++){
if(fast == null) return null;
fast = fast.next;
}
while(fast!=null){
fast = fast.next;
slow = slow.next;
}
return slow;
}