您的位置:首页 > 其它

找到链表中倒数第k个结点

2017-12-08 17:26 155 查看
public static Node findK(Node head,int k){
if(head==null||k==0)return null;
Node first=head;//快指针
Node second=null;//慢指针
for(int i=0;i<k-1;i++){
if(first.next!=null){
first=first.next;
}
else{
return null;
}
}                                                                                                     second=head;
while(first.next!=null){
first=first.next;
second=second.next;
}
return second;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: