Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Definition for ListNode.
- * public class ListNode {
- * int val;
- * ListNode next;
- * ListNode(int val) {
- * this.val = val;
- * this.next = null;
- * }
- * }
- */
- public class Solution {
- /**
- * @param head the head of linked list.
- * @return an integer
- */
- public int countNodes(ListNode head) {
- // Write your code here
- int cnt = 0;
- while (head != null) {
- cnt ++;
- head = head.next;
- }
- return cnt;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment