mirror of
https://github.com/TheAlgorithms/Python.git
synced 2024-11-30 16:31:08 +00:00
Merge pull request #162 from coreywho/master
Recursive solution to insert_tail in singly_LinkedList
This commit is contained in:
commit
438a9c60bc
|
@ -3,22 +3,15 @@ class Node:#create a Node
|
||||||
self.data=data#given data
|
self.data=data#given data
|
||||||
self.next=None#given next to None
|
self.next=None#given next to None
|
||||||
class Linked_List:
|
class Linked_List:
|
||||||
|
|
||||||
pass
|
pass
|
||||||
def insert_tail(Head,data):#insert the data at tail
|
|
||||||
tamp=Head#create a tamp as a head
|
def insert_tail(Head,data):
|
||||||
if(tamp==None):#if linkedlist is empty
|
if(Head.next is None):
|
||||||
newNod=Node()#create newNode Node type and given data and next
|
Head.next = Node(data)
|
||||||
newNod.data=data
|
|
||||||
newNod.next=None
|
|
||||||
Head=newNod
|
|
||||||
else:
|
else:
|
||||||
while tamp.next!=None:#find the last Node
|
insert_tail(Head.next, data)
|
||||||
tamp=tamp.next
|
|
||||||
newNod = Node()#create a new node
|
|
||||||
newNod.data = data
|
|
||||||
newNod.next = None
|
|
||||||
tamp.next=newNod#put the newnode into last node
|
|
||||||
return Head#return first node of linked list
|
|
||||||
def insert_head(Head,data):
|
def insert_head(Head,data):
|
||||||
tamp = Head
|
tamp = Head
|
||||||
if (tamp == None):
|
if (tamp == None):
|
||||||
|
@ -32,16 +25,18 @@ class Linked_List:
|
||||||
newNod.next = Head#put the Head at NewNode Next
|
newNod.next = Head#put the Head at NewNode Next
|
||||||
Head=newNod#make a NewNode to Head
|
Head=newNod#make a NewNode to Head
|
||||||
return Head
|
return Head
|
||||||
def Print(Head):#print every node data
|
|
||||||
tamp=Node()
|
def printList(Head):#print every node data
|
||||||
tamp=Head
|
tamp=Head
|
||||||
while tamp!=None:
|
while tamp!=None:
|
||||||
print(tamp.data)
|
print(tamp.data)
|
||||||
tamp=tamp.next
|
tamp=tamp.next
|
||||||
|
|
||||||
def delete_head(Head):#delete from head
|
def delete_head(Head):#delete from head
|
||||||
if Head!=None:
|
if Head!=None:
|
||||||
Head=Head.next
|
Head=Head.next
|
||||||
return Head#return new Head
|
return Head#return new Head
|
||||||
|
|
||||||
def delete_tail(Head):#delete from tail
|
def delete_tail(Head):#delete from tail
|
||||||
if Head!=None:
|
if Head!=None:
|
||||||
tamp = Node()
|
tamp = Node()
|
||||||
|
@ -50,12 +45,6 @@ class Linked_List:
|
||||||
tamp = tamp.next
|
tamp = tamp.next
|
||||||
tamp.next=None#delete the last element by give next None to 2nd last Element
|
tamp.next=None#delete the last element by give next None to 2nd last Element
|
||||||
return Head
|
return Head
|
||||||
|
|
||||||
def isEmpty(Head):
|
def isEmpty(Head):
|
||||||
if(Head==None):#check Head is None or Not
|
return Head is None #Return if Head is none
|
||||||
return True#return Ture if list is empty
|
|
||||||
else:
|
|
||||||
return False#check False if it's not empty
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user