LF.33.number Of Nodes

Posted davidnyc

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了LF.33.number Of Nodes相关的知识,希望对你有一定的参考价值。

Return the number of nodes in the linked list.

Examples

L = null, return 0
L = 1 -> null, return 1
L = 1 -> 2 -> null, return 2

 

 

 1 public class Solution {
 2   public int numberOfNodes(ListNode head) {
 3     // Write your solution here
 4     if (head == null) {
 5         return 0;
 6     }
 7     int res = 0;
 8     ListNode curr = head ;
 9     while(curr != null){
10         curr = curr.next ;
11         res++;
12     }
13     return res ;
14   }
15 }

 

以上是关于LF.33.number Of Nodes的主要内容,如果未能解决你的问题,请参考以下文章

[GeeksForGeeks] Connect binary tree nodes of same level

[GeeksForGeeks] Remove all half nodes of a given binary tree

[LeetCode 1530] Number of Good Leaf Nodes Pairs

Two nodes of a BST are swapped, correct the BST

[Algo] 646. Store Number Of Nodes In Left Subtree

Making up VMs from Many Weak Nodes of Edge Computing