www

导航

链表中倒数第k个结点

public ListNode FindKthToTail(ListNode head,int k) {
    if(head==null) return head;
    ListNode pre=head;
    ListNode last=head;
    for(int i=0; i<k; i++){
        if(last==null) return null;
        last=last.next;
    }
    while(last!=null){
        pre=pre.next;
        last=last.next;
    }
    return pre;
}

 

posted on 2019-02-25 20:45  www_practice  阅读(128)  评论(0编辑  收藏  举报