查找单链表的倒数第k个结点

 关于单链表的实现 请参考blog

    /*
     * @param herd 链表的头节点
     * @param index 倒数的索引
     * @return 返回对应节点
     * 查找单链表中的倒数第K个节点
     * */
    public static HeroNode findLastIndexNode(HeroNode head,int index){
        if (head.next == null)
            return null;
        int size = getLength(head);
        if (index <= 0 || index > size){
            return null;
        }
        HeroNode cur = head.next;
        for (int i = 0; i < size - index; i++){
            cur = cur.next;
        }
        return cur;
    }

猜你喜欢

转载自blog.csdn.net/weixin_38289787/article/details/103266638