Length of a linked list

Write length(head) that returns how many nodes are in the linked list starting at head. head is a ListNode with fields val and next, or None for an empty list.

length(1 -> 2 -> 3)  ->  3
length(None)         ->  0

The point is to practise walking a linked list with a while loop.

def length(head):
    ...