线性数据结构案例0 —— 单向链表中有效节点个数

一、介绍

 我们假设给定头节点,并且头节点不算有效节点,通过遍历我们得到有效节点个数

二、代码

public int getLength(Node head) {
        if (head.next == null) {
            return 0;
        }
        int length = 0;
        Node temp = head.next;
        while (temp != null) {
            length++;
            temp = temp.next;
        }
        return length;
    }

猜你喜欢

转载自www.cnblogs.com/gary97/p/12288707.html
今日推荐