-
Notifications
You must be signed in to change notification settings - Fork 368
/
linklist_length.py
34 lines (34 loc) · 977 Bytes
/
linklist_length.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#Name : Atul Kumar
#Github username : atul1510
#Repositary name : Algorithms
# Node class
class Node:
def __init__(self, data):
self.data = data
self.next = None
# Function to find the length of a singly linked list
def find_length(head):
count = 0
current = head
while current != None:
count += 1
current = current.next
return count
# Creating an empty linked list
head = None
# Reading input from the user to create the linked list
n = int(input("Enter the number of nodes in the linked list: "))
if n > 0:
prev = None
for i in range(n):
data = int(input("Enter the data for node " + str(i+1) + ": "))
current = Node(data)
if prev == None:
head = current
else:
prev.next = current
prev = current
# Finding the length of the linked list
length = find_length(head)
# Printing the length of the linked list
print("Length of the linked list is:", length)