sweet1cris

Untitled

Feb 9th, 2018
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.54 KB | None | 0 0
  1.  
  2. /**
  3.  * Definition for ListNode.
  4.  * public class ListNode {
  5.  *     int val;
  6.  *     ListNode next;
  7.  *     ListNode(int val) {
  8.  *         this.val = val;
  9.  *         this.next = null;
  10.  *     }
  11.  * }
  12.  */
  13. public class Solution {
  14.     /**
  15.      * @param head the head of linked list.
  16.      * @return an integer
  17.      */
  18.     public int countNodes(ListNode head) {  
  19.         // Write your code here
  20.         int cnt = 0;
  21.         while (head != null) {
  22.             cnt ++;
  23.             head = head.next;
  24.         }
  25.         return cnt;
  26.     }
  27. }
Advertisement
Add Comment
Please, Sign In to add comment