1.输入一个链表,输出该链表中倒数第k个结点。
思路:创建2个指针,第一个从头开始,第二个从第k位开始。当第二个指针达到链表尾的时候,第一个指针指向的节点就是倒数第k个节点。
function FindKthToTail(head, k)
{
// write code here
var count = 0;
var snode = head,enode = head;
while(enode != null){
if(count >= k){
snode = snode.next;
}
count++;
enode = enode.next;
}
if(count < k){
return null;
}else{
return snode;
}
}