Note:
边界的判断自己走一遍即可
代码如下:
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* getKthFromEnd(ListNode* head, int k) {
auto pre = head, tair = head;
while(k --) pre = pre -> next;
while(pre){
pre = pre -> next;
tair = tair -> next;
}
return tair;
}
};