/**
* 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(k){
fast = fast->next;
k--;
}
while(fast){
slow = slow->next;
fast = fast->next;
}
return slow->val;
}
};
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)