from single_linked_list import LinkedList, Node


def getCount(head_node):
    #code here
    cnt = 0
    temp = head_node
    while temp != None:
        cnt += 1
        temp = temp.next
    return cnt


if __name__ == '__main__':
    # t = int(input())
    # for cases in range(t):
    n = int(input())
    a = LinkedList()
    nodes = list(map(int, input().strip().split()))
    for data in nodes:
        node = Node(data)
        a.append(node)
    print(getCount(a.head))
Exemplo n.º 2
0
    """

    if linked_list.is_empty():
        return False

    fast = linked_list.head.next
    slow = linked_list.head.next

    # 快慢指针查找中位数
    while fast is not None and fast.next is not None:
        fast = fast.next.next
        slow = slow.next




if __name__ == '__main__':
    # 创建一个链表对象
    linked_list = LinkedList()

    print('链表是否为空:%d' % linked_list.is_empty())

    linked_list.append('l')
    linked_list.append('e')
    linked_list.append('v')
    linked_list.append('e')
    linked_list.append('l')

    print('遍历单链表:')
    linked_list.traverse()