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

来源:互联网 发布:股票比价关系软件 编辑:程序博客网 时间:2024/05/19 20:37
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;}
原创粉丝点击