https://www.nowcoder.net/practice/529d3ae5a407492994ad2a246518148a?tpId=13&tqId=11167&tPage=1&rp=1&ru=/ta/coding-interviews&qru=/ta/coding-interviews/question-ranking
题目描述
输入一个链表,输出该链表中倒数第k个结点。
代码如下:
/* struct ListNode { int val; struct ListNode *next; ListNode(int x) : val(x), next(NULL) { } };*/ class Solution { public: ListNode* FindKthToTail(ListNode* pListHead, unsigned int k) { if (pListHead == NULL) return NULL; ListNode *pAhead = pListHead; for (int i=0; i<k; i++) { if (pAhead == NULL) return NULL; pAhead = pAhead->next; } ListNode *pCur = pListHead; while (pAhead != NULL) { pAhead = pAhead->next; pCur = pCur->next; } return pCur; } };
结果:
通过
您的代码已保存
答案正确:恭喜!您提交的程序通过了所有的测试用例