Skip to content

Update Linked List from sequence script to use doctests #12766

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Add doctests for the linked list script.
  • Loading branch information
mindaugl committed May 24, 2025
commit a32778b3b6d97b7ee6b7dd64b24cd0f523dca07c
21 changes: 8 additions & 13 deletions data_structures/linked_list/from_sequence.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,29 +20,24 @@ def __repr__(self):
return string_rep


def make_linked_list(elements_list: list) -> Node:
def make_linked_list(elements_list: list | tuple) -> Node:
"""
Creates a Linked List from the elements of the given sequence
(list/tuple) and returns the head of the Linked List.

>>> make_linked_list([1, 3, 5, 32, 44, 12, 43])
<1> ---> <3> ---> <5> ---> <32> ---> <44> ---> <12> ---> <43> ---> <END>
>>> make_linked_list([1])
<1> ---> <END>
>>> make_linked_list((1,))
<1> ---> <END>
"""

# if elements_list is empty
if not elements_list:
raise Exception("The Elements List is empty")

# Set first element as Head
head = Node(elements_list[0])
current = head
# Loop through elements from position 1
for data in elements_list[1:]:
current.next = Node(data)
current = current.next
return head


list_data = [1, 3, 5, 32, 44, 12, 43]
print(f"List: {list_data}")
print("Creating Linked List from List.")
linked_list = make_linked_list(list_data)
print("Linked List:")
print(linked_list)