题解 | #链表中倒数第k个结点#
链表中倒数第k个结点
https://www.nowcoder.com/practice/529d3ae5a407492994ad2a246518148a
/** * struct ListNode { * int val; * struct ListNode *next; * }; */ /** * * @param pListHead ListNode类 * @param k int整型 * @return ListNode类 */ struct ListNode* FindKthToTail(struct ListNode* pListHead, int k ) { if(pListHead==NULL){ return NULL; } struct ListNode* slow,*fast; fast=pListHead; slow=pListHead; while (k) { fast=fast->next; if(fast==NULL&&k!=1)//如果k!=1,就说明k超出了链表的长度 { return NULL; } k--; } while(fast){ fast=fast->next; slow=slow->next; } return slow; }