-
-
Notifications
You must be signed in to change notification settings - Fork 46.7k
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
Create sorted_linked_list.py #11847
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
class Node: | ||
def __init__(self, data): | ||
self.data = data | ||
self.next = None | ||
|
||
class SortedLinkedList: | ||
def __init__(self): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please provide return type hint for the function: |
||
self.head = None | ||
|
||
def insert(self, data): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please provide return type hint for the function: As there is no test file in this pull request nor any test function or class in the file Please provide type hint for the parameter: |
||
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): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please provide return type hint for the function: As there is no test file in this pull request nor any test function or class in the file |
||
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.") | ||
# Display the sorted linked list | ||
print("Sorted Linked List:") | ||
sll.display() |
There was a problem hiding this comment.
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