题目来源:
题目描述:
输入一个链表,输出该链表中倒数第k个结点。
代码如下:
/*
public class ListNode {
int val;
ListNode next = null;
ListNode(int val) {
this.val = val;
}
}*/
public class Solution {
public ListNode FindKthToTail(ListNode head, int k) {
ListNode fast = head;
ListNode slow = head;
int i = 0;
for (; fast != null; i++) {
fast = fast.next;
if (i >= k) {
slow = slow.next;
}
}
return i < k ? null : slow;
}
}