用的是快慢指针的思想
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
int kthToLast(ListNode* head, int k) {
ListNode* fast = head;
ListNode* slow = head;
while(fast != nullptr)
{
fast = fast->next;
if(k == 0)
{
slow = slow->next;
}
else
{
k--;
}
}
return slow->val;
}
};