For example, the function should return 5 for linked list [1 -> 3 -> 1 -> 2 -> 1]
-
- Initialize count as 0
-
- Initialize a node pointer, current = head.
-
- Do following while current is not NULL a) current = current -> next b) count++;
-
- Return count
- Input: Linked List =
[1 -> 3 -> 1 -> 2 -> 1]
- Output: count of nodes is
5
Algorithm Complexity
Complexity | Notation |
---|---|
Time Complexity |
O(n) |