版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/github_37315869/article/details/82974151
题目描述
输入一个链表,输出该链表中倒数第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 null;
int count=1;
ListNode p=head;
while(p.next!=null){
count++;
p=p.next;
}
if(k>count)
return null;
count-=k-1;
p=head;
while(count!=1){
p=p.next;
count--;
}
return p;
}
}