Skip to content

Commit 4b74bf3

Browse files
committed
implementing a similar approch from remove() to pop_tail() toremove the last nodes
1 parent 09df9bb commit 4b74bf3

File tree

1 file changed

+10
-5
lines changed

1 file changed

+10
-5
lines changed

Sprint-2/implement_linked_list/linked_list.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,22 @@ def push_head(self, value): #this is to add a value/node to the head/start.
1616
if not self.tail:
1717
self.tail = node #list=empty,tail also points to new node
1818
return node
19-
def pop_tail(self): #Remove the value from the tail of the list
19+
def pop_tail(self):
2020
if not self.tail:
21-
return None
22-
value = self.tail.value
23-
prev_node = self.tail.previous
21+
return None
22+
removed_node = self.tail
23+
value = removed_node.value
24+
prev_node = removed_node.previous
2425
if prev_node:
2526
prev_node.next = None
27+
2628
self.tail = prev_node
2729
if not self.tail:
28-
self.head = None #If only one element is removed.
30+
self.head = None
31+
removed_node.previous = None
32+
removed_node.next = None
2933
return value
34+
3035
def remove(self, node): #remove any node
3136
if not node:
3237
return

0 commit comments

Comments
 (0)