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 }

 

posted @ 2018-03-27 12:29  davidnyc  阅读(162)  评论(0编辑  收藏  举报