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 }
原文地址:https://www.cnblogs.com/davidnyc/p/8656790.html