分两种情况,第一种是当链表长度小于k时,直接返回空,第二种情况是大于等于k时,一直遍历到链表长度减k即可。
public int length(Node head)
{
Node cur=head;
int len=0;
while(cur!=null)
{
cur=cur.next;
len++;
}
return len;
}
public Node findKthTotail(Node head,int k)
{
int len=length(head);
if(k<len)
{
return null;
}
else{
Node cur=head;
for(int i=0;i<len-k;i++)
{
cur=cur.next;
}
return cur;
}
}