题解 | #链表中倒数最后k个结点#
链表中倒数最后k个结点
https://www.nowcoder.com/practice/886370fe658f41b498d40fb34ae76ff9
struct ListNode* FindKthToTail(struct ListNode* pHead, int k) { struct ListNode *p = NULL; //定义遍历指针 p = pHead; //指向首结点 int n = 0; //计算链表的长度 while(p) { p = p->next; n++; } if(k>n) //如果第k个结点不在链表长度范围就结束 { return 0; } p = pHead; for(int i=0;i<(n-k);i++)//当i<(n-k)时退出来,此时指针指向k结点 { p = p->next; } return p; }