Skip to content

Commit ebf2c54

Browse files
committed
chore: update print_reverse helper method
Use a generator expression instead of slicing `elements_list` to improve the space and time complexity of `make_linked_list` to O(1) space and O(n) time by avoiding the creation a shallow copy of `elements_list`.
1 parent b934da4 commit ebf2c54

File tree

1 file changed

+4
-2
lines changed

1 file changed

+4
-2
lines changed

data_structures/linked_list/print_reverse.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,10 @@ def make_linked_list(elements_list):
2828
# Set first element as Head
2929
head = Node(elements_list[0])
3030
current = head
31-
# Loop through elements from position 1
32-
for data in elements_list[1:]:
31+
list_length = len(elements_list)
32+
33+
# Iterate over values from generator expression starting from position 1
34+
for data in (elements_list[i] for i in range(1, list_length)):
3335
current.next = Node(data)
3436
current = current.next
3537
return head

0 commit comments

Comments
 (0)