面试题 02.02
/**
* 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 *cur = head;
while (k-- && cur) {
cur = cur->next;
}
while (cur) {
head = head->next;
cur = cur->next;
}
return head->val;
}
};Last updated