Skip to content
Closed
Changes from all commits
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
49 changes: 49 additions & 0 deletions data_structures/linked_list/sorted_linked_list.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
class Node:
def __init__(self, data):
self.data = data
self.next = None

class SortedLinkedList:
def __init__(self):
self.head = None

def insert(self, data):
new_node = Node(data)

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

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W293)

data_structures/linked_list/sorted_linked_list.py:12:1: W293 Blank line contains whitespace
# 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):
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 46 in data_structures/linked_list/sorted_linked_list.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W293)

data_structures/linked_list/sorted_linked_list.py:46:1: W293 Blank line contains whitespace
# Display the sorted linked list
print("Sorted Linked List:")
sll.display()
Loading