小练习:lintcode466. 链表节点计数

题目:

计算链表中有多少个节点.

样例:

给出 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

你可能感兴趣的:(lintcode)