版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/weixin_38158541/article/details/85252824
时间限制:1秒 空间限制:32768K 热度指数:512101
本题知识点: 链表
题目描述
输入一个链表,输出该链表中倒数第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) {
if (head == null) return head;
ListNode node = head;
int count = 0;
while (node != null) {
count++;
node = node.next;
}
if (count < k) return null;
ListNode p = head;
for(int i = 0; i < count - k; i++){
p = p.next;
}
return p;
}
}