"""
Definition of ListNode
class ListNode(object):
def __init__(self, val, next=None):
self.val = val
self.next = next
"""
class Solution:
"""
@param head: the first node of linked list.
@return: An integer
"""
def countNodes(self, head):
# write your code here
n = 0
ptr = head
while ptr != None:
n = n + 1
ptr = ptr.next
return nPython, LintCode, 466. 链表节点计数
最新推荐文章于 2021-08-06 15:33:05 发布
本文介绍了一种通过遍历链表来计算链表中节点数量的方法。定义了链表节点类`ListNode`并实现了一个`countNodes`函数,该函数接受链表头结点作为参数,并返回链表中的节点总数。
3万+

被折叠的 条评论
为什么被折叠?



