题解 | #链表中倒数最后k个结点#
链表中倒数最后k个结点
https://www.nowcoder.com/practice/886370fe658f41b498d40fb34ae76ff9
//通过遍历求出链表的长度,然后再减去k来求得目标节点的位序
struct ListNode* FindKthToTail(struct ListNode* pHead, int k ) {
// write code here
if(pHead==NULL)
{
return pHead;
}
struct ListNode* cur=pHead;
int leng=0,num=0;
while(cur)
{
cur=cur->next;
leng++;
}
if(k>leng)
{
return NULL;
}
num=leng-k;
cur=pHead;
while(num--)
{
cur=cur->next;
}
return cur;
}