Problem statement

https://binarysearch.com/problems/Length-of-a-Linked-List/

Solution

Just traverse list.

Complexity

It is O(n) for time and O(1) for space.

Code

class Solution:
    def solve(self, node):
        n = 0
        while node:
            node = node.next
            n += 1
        return n