Skip to content

Commit 00c13c2

Browse files
authored
Merge pull request TheAlgorithms#243 from cclauss/patch-1
Fix unresolved name: insert_tail()
2 parents bc34f6e + a88ad60 commit 00c13c2

File tree

1 file changed

+40
-38
lines changed

1 file changed

+40
-38
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,67 +1,69 @@
11
from __future__ import print_function
2-
class Node:#create a Node
3-
def __int__(self,data):
4-
self.data=data#given data
5-
self.next=None#given next to None
2+
3+
4+
class Node: # create a Node
5+
def __int__(self, data):
6+
self.data = data # given data
7+
self.next = None # given next to None
8+
9+
610
class Linked_List:
7-
8-
pass
9-
10-
def insert_tail(Head,data):
11-
if(Head.next is None):
11+
def insert_tail(Head, data):
12+
if Head.next is None:
1213
Head.next = Node(data)
1314
else:
14-
insert_tail(Head.next, data)
15+
Head.next.insert_tail(data)
1516

16-
def insert_head(Head,data):
17+
def insert_head(Head, data):
1718
tamp = Head
18-
if (tamp == None):
19-
newNod = Node()#create a new Node
19+
if tamp is None:
20+
newNod = Node() # create a new Node
2021
newNod.data = data
2122
newNod.next = None
22-
Head = newNod#make new node to Head
23+
Head = newNod # make new node to Head
2324
else:
2425
newNod = Node()
2526
newNod.data = data
26-
newNod.next = Head#put the Head at NewNode Next
27-
Head=newNod#make a NewNode to Head
28-
return Head
29-
30-
def printList(Head):#print every node data
31-
tamp=Head
32-
while tamp!=None:
27+
newNod.next = Head # put the Head at NewNode Next
28+
Head = newNod # make a NewNode to Head
29+
return Head
30+
31+
def printList(Head): # print every node data
32+
tamp = Head
33+
while tamp is not None:
3334
print(tamp.data)
34-
tamp=tamp.next
35-
36-
def delete_head(Head):#delete from head
37-
if Head!=None:
38-
Head=Head.next
39-
return Head#return new Head
40-
41-
def delete_tail(Head):#delete from tail
42-
if Head!=None:
35+
tamp = tamp.next
36+
37+
def delete_head(Head): # delete from head
38+
if Head is not None:
39+
Head = Head.next
40+
return Head # return new Head
41+
42+
def delete_tail(Head): # delete from tail
43+
if Head is not None:
4344
tamp = Node()
4445
tamp = Head
45-
while (tamp.next).next!= None:#find the 2nd last element
46+
while tamp.next.next is not None: # find the 2nd last element
4647
tamp = tamp.next
47-
tamp.next=None#delete the last element by give next None to 2nd last Element
48+
# delete the last element by give next None to 2nd last Element
49+
tamp.next = None
4850
return Head
4951

5052
def isEmpty(Head):
51-
return Head is None #Return if Head is none
52-
53+
return Head is None # Return if Head is none
54+
5355
def reverse(Head):
5456
prev = None
5557
current = Head
56-
57-
while(current):
58-
# Store the current node's next node.
58+
59+
while current:
60+
# Store the current node's next node.
5961
next_node = current.next
6062
# Make the current node's next point backwards
6163
current.next = prev
6264
# Make the previous node be the current node
6365
prev = current
6466
# Make the current node the next node (to progress iteration)
6567
current = next_node
66-
# Return prev in order to put the head at the end
68+
# Return prev in order to put the head at the end
6769
Head = prev

0 commit comments

Comments
 (0)