(指针链表) $O(N)$
指针指向下一个。
时间复杂度
$O(N)$
参考文献
无
C++ 代码
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* findKthToTail(ListNode* a, int k) {
auto s = a;
for (int i = 0; i < k; i++) {
if (s)
s = s->next;
if (!s && i < k - 1)
return NULL;
}
while (s) {
a = a->next;
s = s->next;
}
return a;
}
};