Skip to content

Commit a46a253

Browse files
Update from_sequence.py
1 parent 251fcba commit a46a253

File tree

1 file changed

+17
-3
lines changed

1 file changed

+17
-3
lines changed

data_structures/linked_list/from_sequence.py

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,18 +25,32 @@ def make_linked_list(elements_list: list | tuple) -> Node:
2525
Creates a Linked List from the elements of the given sequence
2626
(list/tuple) and returns the head of the Linked List.
2727
28-
>>> make_linked_list([1, 3, 5, 32, 44, 12, 43])
29-
<1> ---> <3> ---> <5> ---> <32> ---> <44> ---> <12> ---> <43> ---> <END>
28+
>>> make_linked_list([])
29+
Traceback (most recent call last):
30+
...
31+
ValueError: The Elements List is empty
32+
>>> make_linked_list(())
33+
Traceback (most recent call last):
34+
...
35+
ValueError: The Elements List is empty
3036
>>> make_linked_list([1])
3137
<1> ---> <END>
3238
>>> make_linked_list((1,))
3339
<1> ---> <END>
40+
>>> make_linked_list([1, 3, 5, 32, 44, 12, 43])
41+
<1> ---> <3> ---> <5> ---> <32> ---> <44> ---> <12> ---> <43> ---> <END>
42+
>>> make_linked_list((1, 3, 5, 32, 44, 12, 43))
43+
<1> ---> <3> ---> <5> ---> <32> ---> <44> ---> <12> ---> <43> ---> <END>
3444
"""
3545

46+
# if elements_list is empty
3647
if not elements_list:
37-
raise Exception("The Elements List is empty")
48+
raise ValueError("The Elements List is empty")
49+
50+
# Set first element as Head
3851
head = Node(elements_list[0])
3952
current = head
53+
# Loop through elements from position 1
4054
for data in elements_list[1:]:
4155
current.next = Node(data)
4256
current = current.next

0 commit comments

Comments
 (0)