Skip to content

Commit 7abd848

Browse files
committed
update pop_head, remove unnecessary append to tail call
1 parent 7d9ae3e commit 7abd848

File tree

1 file changed

+4
-5
lines changed

1 file changed

+4
-5
lines changed

Python/chapter02/2.4 - Partition/miguel_2.4_soln.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -85,15 +85,18 @@ def get_node_at(self, index: int) -> Node:
8585

8686
def pop_head(self) -> Optional[Node]:
8787
if self.head is None:
88-
return self.head
88+
raise IndexError('no head to pop')
8989
h = self.head
90+
h.next = None
9091
self.head = self.head.next
92+
self.size -= 1
9193
return h
9294

9395
def append(self, ll: object):
9496
if not isinstance(ll, LinkedList):
9597
raise TypeError('not a linked list')
9698
self.tail.next = ll.head
99+
self.size += ll.size
97100

98101
def __repr__(self):
99102
return self.__str__()
@@ -178,10 +181,6 @@ def partition_ll(ll: LinkedList, pivot: int) -> LinkedList:
178181
else:
179182
right_partition.append_to_tail(ll.pop_head())
180183
n = n.next
181-
# last element may still be pointing
182-
# to earlier elements that are less than
183-
# pivot so we need to cut that link
184-
right_partition.append_to_tail(ll.pop_head())
185184
# then, merge left and right partition lists into one linked list
186185
left_partition.append(right_partition)
187186
return left_partition

0 commit comments

Comments
 (0)