Given the head of a singly linked list of integers, find and return its length.

The length of the list is 4. Hence we return 4.
Exercise caution when dealing with edge cases, such as when the head is NULL. Failing to handle these edge cases appropriately may result in a runtime error in your code.
The first and only line contains elements of the singly linked list separated by a single space, -1 indicates the end of the singly linked list and hence, would never be a list element.
Return a single integer denoting the length of the linked list.
3 4 5 2 6 1 9 -1
7
The number of nodes in the given linked list is 7.
Hence we return 7.
10 76 39 -3 2 9 -23 9 -1
8
Try to do this in O(n).
0 <= N <= 10^5
Time Limit: 1 sec
Linearly traverse the linked list.
The basic idea of this approach is to traverse the linked list.
Now consider the following steps:
O(N), where ‘N’ is the total number of nodes.
We are iterating the linked list. So the overall time complexity will be O(N).
O(1)
We are using constant extra space. Hence, the overall space complexity is O(1).