public class Solution {
public ListNode FindKthToTail(ListNode head,int k) {
if(head==null||k<0){ //条件不符合直接返回空
return null;
}
ListNode cur=head;
int length=0;//求长度
for(cur=head;cur!=null;cur=cur.next)
{
length++;
}
if (k > length)
{
return null;
}
cur=head;
int len=length-(k-1);
for(int i=1;i<len;i++)//遍历输出
{
cur=cur.next;
}
return cur;
}
}
输出链表的倒数第k个节点
最新推荐文章于 2024-03-22 12:38:53 发布