class Solution {
public:
ListNode* getKthFromEnd(ListNode* head, int k) {
int n = 0;
auto node = head;
while (node) {
node = node->next;
n++;
}
for (int i = 0; i < n; i++) {
if (i == n - k) return head;
head = head->next;
}
return nullptr;
}
};
class Solution {
public:
ListNode* getKthFromEnd(ListNode* head, int k) {
auto preK = head;
auto cur = head;
for (int i = 0; i < k; i++)
cur = cur->next;
while (cur) {
cur = cur->next;
preK = preK->next;
}
return preK;
}
};