注意检查链表是否为空。时间复杂度为O(n)。这个比较简单。

 核心代码:

// 方法:获取单链表的长度
    public int getLength(Node head) {
        if (head == null) {
            return 0;
        }
        int length = 0;
        Node current = head;
        while (current != null) {
            length++;
            current = current.next;
        }
        return length;
    }

 

相关文章:

  • 2021-07-24
  • 2021-10-26
  • 2021-09-13
  • 2022-03-10
  • 2021-06-22
  • 2021-12-05
  • 2022-12-23
  • 2021-06-13
猜你喜欢
  • 2022-01-22
  • 2022-12-23
  • 2021-11-26
  • 2022-01-01
  • 2022-12-23
  • 2021-06-28
相关资源
相似解决方案