Skip to content

Create sorted_linked_list.py #11847

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

Closed
wants to merge 2 commits into from
Closed
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
46 changes: 46 additions & 0 deletions data_structures/linked_list/sorted_linked_list.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
class Node:
def __init__(self, data):

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please provide return type hint for the function: __init__. If the function does not return a value, please provide the type hint as: def function() -> None:

Please provide type hint for the parameter: data

self.data = data
self.next = None

class SortedLinkedList:
def __init__(self):

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please provide return type hint for the function: __init__. If the function does not return a value, please provide the type hint as: def function() -> None:

self.head = None

def insert(self, data):

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please provide return type hint for the function: insert. If the function does not return a value, please provide the type hint as: def function() -> None:

As there is no test file in this pull request nor any test function or class in the file data_structures/linked_list/sorted_linked_list.py, please provide doctest for the function insert

Please provide type hint for the parameter: data

new_node = Node(data)
# Case when the list is empty or the new node needs to be at the head
if self.head is None or self.head.data >= new_node.data:
new_node.next = self.head
self.head = new_node
else:
# Find the correct position to insert the new node
current = self.head
while current.next is not None and current.next.data < new_node.data:
current = current.next
new_node.next = current.next
current.next = new_node

def display(self):

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please provide return type hint for the function: display. If the function does not return a value, please provide the type hint as: def function() -> None:

As there is no test file in this pull request nor any test function or class in the file data_structures/linked_list/sorted_linked_list.py, please provide doctest for the function display

current = self.head
while current:
print(current.data, end=" -> ")
current = current.next
print("None")

# Create an instance of SortedLinkedList
sll = SortedLinkedList()
# Take input from the user
while True:
try:
user_input = input("Enter a number (or 'q' to quit): ")
if user_input == 'q':
break
else:
num = int(user_input)
sll.insert(num)
except ValueError:
print("Please enter a valid number or 'q' to quit.")

Check failure on line 43 in data_structures/linked_list/sorted_linked_list.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W291)

data_structures/linked_list/sorted_linked_list.py:43:61: W291 Trailing whitespace
# Display the sorted linked list
print("Sorted Linked List:")
sll.display()
Loading