Skip to content

Commit fcd5ac1

Browse files
Update singly_LinkedList.py
1 parent d5fd222 commit fcd5ac1

File tree

1 file changed

+7
-29
lines changed

1 file changed

+7
-29
lines changed

data_structures/LinkedList/singly_LinkedList.py

Lines changed: 7 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -9,37 +9,36 @@ def __int__(self,data):
99
class Linked_List:
1010
pass
1111
def insert_tail(Head,data): #insert the data at tail
12-
tamp=Head
13-
if(tamp==None):
12+
tamp=Head #create a tamp as a head
13+
if(tamp==None): # if linkedlist is empty
1414
newNod=Node() #create newNode Node type and given data and next
1515
newNod.data=data
1616
newNod.next=None
1717
Head=newNod
1818

1919
else:
20-
while tamp.next!=None: #reaches the last Node
20+
while tamp.next!=None: #find the last Node
2121
tamp=tamp.next
2222
newNod = Node() #create a new node
2323
newNod.data = data
2424
newNod.next = None
2525
tamp.next=newNod #put the newnode into last node
2626

27-
return Head
27+
return Head #return first node of linked list
2828
def insert_head(Head,data):
2929
tamp = Head
3030
if (tamp == None):
3131
newNod = Node() #create a new Node
3232
newNod.data = data
3333
newNod.next = None
3434
Head = newNod #make new node to Head
35-
# print(Head.data)
3635
return Head
3736
else:
3837
newNod = Node()
3938
newNod.data = data
4039
newNod.next = Head #put the Head at NewNode Next
4140
Head=newNod # make a NewNode to Head
42-
# print(tamp.data)
41+
4342
return Head
4443

4544

@@ -70,9 +69,9 @@ def delete_tail(Head): #delete from tail
7069
else:
7170
tamp = Node()
7271
tamp = Head
73-
while (tamp.next).next!= None: #reach tha 2nd last element
72+
while (tamp.next).next!= None: #find the 2nd last element
7473
tamp = tamp.next
75-
tamp.next=None #delet the last element by give next None to 2nd last Element
74+
tamp.next=None #delete the last element by give next None to 2nd last Element
7675

7776

7877

@@ -86,25 +85,4 @@ def isEmpty(Head):
8685

8786

8887

89-
##check
90-
91-
Head=None
92-
Head=Linked_List.insert_tail(Head,5)
93-
Head=Linked_List.insert_tail(Head,6)
94-
Head=Linked_List.insert_head(Head,7)
95-
Head=Linked_List.insert_head(Head,9)
96-
97-
Linked_List.Print(Head)
98-
99-
print("delete_tail")
100-
Linked_List.delete_tail(Head)
101-
Linked_List.Print(Head)
102-
103-
104-
print("delete_head")
105-
Head=Linked_List.delete_head(Head)
106-
Linked_List.Print(Head)
107-
108-
Linked_List.isEmpty(Head)
109-
11088

0 commit comments

Comments
 (0)