1.题目链接
2.题目描述
输入一个链表,输出该链表中倒数第k个结点。
3.题解
/*
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 slow =head;
ListNode fast =head;
int n=0;
while(n<k){
if(fast==null){return null;}
fast=fast.next;
n++;
}
while(fast!=null){
slow=slow.next;
fast=fast.next;
}
return slow;
}
}