Skip to content

Commit fe92d53

Browse files
authored
Merge pull request #252 from Prateekatma/master
Update Singly_Linked_List.py
2 parents 04d2422 + 913e96b commit fe92d53

File tree

1 file changed

+17
-11
lines changed

1 file changed

+17
-11
lines changed

Data_Structure/src/Linked Lists/Singly_Linked_List.py

+17-11
Original file line numberDiff line numberDiff line change
@@ -5,39 +5,43 @@
55
@author: mishr
66
"""
77

8-
#Singly Linked List
8+
#Singly Linked List
9+
#Node class
910

1011
class Node:
11-
12+
# Function to initialise the node object
1213
def __init__(self, data=None, next_node=None):
1314
self.data = data
1415
self.next_node = next_node
1516

16-
17+
# Linked List class contains a Node object
1718

1819
class singly_linked_list:
20+
# Function to initialize head
1921
def __init__(self):
20-
2122
self.head = Node()
22-
23+
24+
# Functio to insert a new node
2325
def append(self, data):
2426

2527
new_node = Node(data)
2628
current = self.head
2729
while current.next_node != None:
2830
current = current.next_node
2931
current.next_node = new_node
30-
32+
33+
# Functio to know the length of the linked list
3134
def length(self):
3235

3336
current = self.head
34-
counter = 0
37+
count = 0
3538
while current.next_node != None:
36-
counter += 1
39+
count += 1
3740
current = current.next_node
3841

39-
return counter
42+
return count
4043

44+
# Functio to get value at a position in the linked list
4145
def get_value_at(self, index):
4246

4347
if index >= self.length():
@@ -51,6 +55,7 @@ def get_value_at(self, index):
5155
return current_node.data
5256
current_index += 1
5357

58+
# Functio to delete the node in the linked list
5459
def delete_at(self, index):
5560

5661
if index >= self.length():
@@ -65,7 +70,8 @@ def delete_at(self, index):
6570
last_node.next_node = current_node.next_node
6671
return
6772
current_index += 1
68-
73+
74+
# Functio to traverse and show the linked list
6975
def display(self):
7076

7177
node_list = []
@@ -91,4 +97,4 @@ def display(self):
9197
#can use loop to take inputs
9298

9399

94-
100+

0 commit comments

Comments
 (0)