#Linked List Count node program
class Node:
def __init__(self, data):
self.data =data
self.next = None
class LinkedList:
def __init__(self):
self.head = None
def push(self, new_data):
new_node = Node(new_data)
new_node.next = self.head
self.head = new_node
def getCount(self):
temp = self.head
count = 0
while temp:
count += 1
temp = temp.next
return count
'''
#recursively function
def getCountRec(self, node):
if (not node): # Base case
return 0
else:
return 1 + self.getCountRec(node.next)
# A wrapper over getCountRec()
def getCount(self):
return self.getCountRec(self.head)
'''
if __name__ == '__main__':
llist = LinkedList()
llist.push(1)
llist.push(2)
llist.push(3)
llist.push(4)
result = llist.getCount()
print('Count of nodes is :', result)
0 Comments