题目:
计算链表中有多少个节点.
##样例:
给出 1->3->5, 返回 3.
就很简单的练习
"""
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
i = 0
p = head
while(p):
i += 1
p = p.next
return i
链表节点计数
本文介绍了一种简单的方法来计算链表中节点的数量,并提供了一个Python实现示例。
2612

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



