Skip to content

Commit 438a9c6

Browse files
authored
Merge pull request #162 from coreywho/master
Recursive solution to insert_tail in singly_LinkedList
2 parents 6bc30c7 + 86a5b03 commit 438a9c6

File tree

1 file changed

+13
-24
lines changed

1 file changed

+13
-24
lines changed

Diff for: data_structures/LinkedList/singly_LinkedList.py

+13-24
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,15 @@ def __int__(self,data):
33
self.data=data#given data
44
self.next=None#given next to None
55
class Linked_List:
6+
67
pass
7-
def insert_tail(Head,data):#insert the data at tail
8-
tamp=Head#create a tamp as a head
9-
if(tamp==None):#if linkedlist is empty
10-
newNod=Node()#create newNode Node type and given data and next
11-
newNod.data=data
12-
newNod.next=None
13-
Head=newNod
8+
9+
def insert_tail(Head,data):
10+
if(Head.next is None):
11+
Head.next = Node(data)
1412
else:
15-
while tamp.next!=None:#find the last Node
16-
tamp=tamp.next
17-
newNod = Node()#create a new node
18-
newNod.data = data
19-
newNod.next = None
20-
tamp.next=newNod#put the newnode into last node
21-
return Head#return first node of linked list
13+
insert_tail(Head.next, data)
14+
2215
def insert_head(Head,data):
2316
tamp = Head
2417
if (tamp == None):
@@ -32,16 +25,18 @@ def insert_head(Head,data):
3225
newNod.next = Head#put the Head at NewNode Next
3326
Head=newNod#make a NewNode to Head
3427
return Head
35-
def Print(Head):#print every node data
36-
tamp=Node()
28+
29+
def printList(Head):#print every node data
3730
tamp=Head
3831
while tamp!=None:
3932
print(tamp.data)
4033
tamp=tamp.next
34+
4135
def delete_head(Head):#delete from head
4236
if Head!=None:
4337
Head=Head.next
4438
return Head#return new Head
39+
4540
def delete_tail(Head):#delete from tail
4641
if Head!=None:
4742
tamp = Node()
@@ -50,12 +45,6 @@ def delete_tail(Head):#delete from tail
5045
tamp = tamp.next
5146
tamp.next=None#delete the last element by give next None to 2nd last Element
5247
return Head
53-
def isEmpty(Head):
54-
if(Head==None):#check Head is None or Not
55-
return True#return Ture if list is empty
56-
else:
57-
return False#check False if it's not empty
58-
59-
60-
6148

49+
def isEmpty(Head):
50+
return Head is None #Return if Head is none

0 commit comments

Comments
 (0)