题解 | #链表中倒数第k个结点#
链表中倒数第k个结点
http://www.nowcoder.com/practice/529d3ae5a407492994ad2a246518148a
struct ListNode* FindKthToTail(struct ListNode* pListHead, int k ) {
// write code here
if(pListHead==NULL){//如果链表为空,直接返回NULL
return NULL;
}
struct ListNode*fast=pListHead;
struct ListNode*slow=fast;
while(k--){
if(fast==NULL){//如果k的值大于链表长度直接返回NULL
return NULL;
}
fast=fast->next;
}
while(fast!=NULL){
fast=fast->next;
slow=slow->next;
}
return slow;
}