原题目:https://leetcode-cn.com/problems/lian-biao-zhong-dao-shu-di-kge-jie-dian-lcof/
思路:
采用双指针,第一个指针先走k步。然后一起走,那么这两个指针就一直保持k的距离,之后先走的指针到头(NULL),返回后走的那一个指针就可以了。
代码;
class Solution {
public:
ListNode* getKthFromEnd(ListNode* head, int k) {
ListNode* latter=head,*former=head;
int t=0;
while(former){
if(t>=k) latter = latter->next;
former = former->next;
t++;
}
return latter;
}
};