计算一个链表中有多少个节点
参考代码:
class ListNode(object): def __init__(self, val, next = None) -> None: self.val = val self.next = next class Solution: def countNodes(self, head): cnt = 0 while head is not None: cnt += 1 head = head.next return cnt