#yyds干货盘点# LeetCode程序员面试金典:返回倒数第 k 个节点

时间:2022-12-07 19:02:34

题目:

实现一种算法,找出单向链表中倒数第 k 个节点。返回该节点的值。

注意:本题相对原题稍作改动

示例:

输入: 1->2->3->4->5 和 k = 2

输出: 4

代码实现:

/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public int kthToLast(ListNode head, int k) {
ListNode first = head;
ListNode second = head;
//第一个指针先走k步
while (k-- > 0) {
first = first.next;
}
//然后两个指针在同时前进
while (first != null) {
first = first.next;
second = second.next;
}
return second.val;
}


}