Skip to content

Commit d4ba122

Browse files
committed
refactor: reuse remove() in pop_tail to avoid duplication
1 parent e1ef332 commit d4ba122

1 file changed

Lines changed: 5 additions & 13 deletions

File tree

Sprint-2/implement_linked_list/linked_list.py

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
class Node:
22
"""
3-
A node in a double linked list.
3+
A node in a doubly linked list.
44
55
Each node supports a data, previous, and next
66
- data: the value
77
- previous: pointer to the previous node
88
- next: pointer to the next node
99
"""
10+
_slots_ = ("data", "previous", "next")
1011

1112
def __init__(self, data):
1213
self.data = data
@@ -69,19 +70,10 @@ def pop_tail(self):
6970
raise ValueError("List is empty.")
7071

7172
# store value before removing node
72-
value = self.tail.data
73+
node = self.tail
74+
value = node.data
7375

74-
if self.head == self.tail:
75-
# Only one element in the list
76-
self.head = None
77-
self.tail = None
78-
79-
else:
80-
# Move tail pointer backwards
81-
self.tail = self.tail.previous
82-
83-
# Disconnect old tail node
84-
self.tail.next = None
76+
self.remove(node)
8577

8678
return value
8779

0 commit comments

Comments
 (0)