版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/weixin_44643195/article/details/103274826
题目描述
输入一个链表,输出该链表中倒数第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;
int count=0;
ListNode node=head;
while(node!=null){
node=node.next;
count++;
}
if(count<k)
return null;
ListNode res=head;
for(int i=0;i<count-k;i++){
res=res.next;
}
return res;
}
}