Skip to content

Commit cc6d2b0

Browse files
committed
length of list
1 parent c3d0387 commit cc6d2b0

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

length of the list.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
class Node:
2+
def __init__(self, data):
3+
self.data = data
4+
self.next = None
5+
6+
7+
class LinkedList:
8+
def __init__(self):
9+
self.head = None
10+
11+
def countNode(self):
12+
temp = self.head
13+
count = 0
14+
while temp is not None:
15+
count = count + 1 # we can also write it like count += 1
16+
temp = temp.next
17+
return count
18+
19+
def printList(self):
20+
temp = self.head
21+
while temp is not None:
22+
print(temp.data)
23+
temp = temp.next
24+
25+
26+
l1 = LinkedList()
27+
l1.head = Node(10)
28+
l2 = Node(20)
29+
l3 = Node(30)
30+
l4 = Node(40)
31+
l1.head.next = l2
32+
l2.next = l3
33+
l3.next = l4
34+
l1.printList()
35+
print("the length of the linked list is :", l1.countNode())

0 commit comments

Comments
 (0)