题解 | #链表中倒数第k个结点#
链表中倒数第k个结点
https://www.nowcoder.com/practice/529d3ae5a407492994ad2a246518148a
/*
struct ListNode {
int val;
struct ListNode *next;
ListNode(int x) :
val(x), next(NULL) {
}
};*/
class Solution {
public:
ListNode* FindKthToTail(ListNode* head, unsigned int k) {
if (k < 0) return nullptr;
ListNode* temp = head;
while (k -- && temp) temp = temp->next;
if (k != -1) return nullptr;
while (temp) {
temp = temp->next;
head = head->next;
}
return head;
}
};