/** * struct ListNode { * int val; * struct ListNode *next; * }; */ /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param pHead ListNode类 * @param k int整型 * @return ListNode类 */ struct ListNode* FindKthToTail(struct ListNode* pHead, int k ) { //思路 //遍历链表计算总长度size,再从头遍历size-k个结点,即为所求 struct ListNode *pTmp = pHead; int size = 0; if ((pHead == NULL) || (k <= 0)) { return NULL; } //遍历链表计算总长度size while (pTmp != NULL) { pTmp = pTmp->next; size++; } if (size < k) { return NULL; } pTmp = pHead; //从头遍历size - k个结点 for (size_t i = 0; i < size - k; i++) { pTmp = pTmp->next; } return pTmp; }