【题目】
实现一种算法,找出单向链表中倒数第 k 个节点。返回该节点的值。注意:本题相对原题稍作改动。
来源:leetcode
链接:https://leetcode-cn.com/problems/kth-node-from-end-of-list-lcci/
【示例1】
输入: 1->2->3->4->5 和 k = 2
输出: 4
【说明】
给定的 k 保证是有效的。
【代码】
耗时:4ms 75.22%
内存:10.5MB 100%
/**
* 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 *pre=head,*last=head;
while(k--){
last=last->next;
}
while(last){
last=last->next;
pre=pre->next;
}
return pre->val;
}
};