美文网首页
466. Count Linked List Nodes

466. Count Linked List Nodes

作者: fdgump | 来源:发表于2018-01-11 11:42 被阅读0次

    [ http://www.lintcode.com/en/problem/count-linked-list-nodes/ ]

    # Python
    """
    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
            p = head
            count = 0
            while p != None:
                p = p.next
                count += 1
            return count
    

    相关文章

      网友评论

          本文标题:466. Count Linked List Nodes

          本文链接:https://www.haomeiwen.com/subject/rfpznxtx.html