From 2977c61c9cc6dcff8eefdc7d70060d758b23df8d Mon Sep 17 00:00:00 2001 From: JAHNAVI THONDEPU <95975610+jahnaviT2003@users.noreply.github.com> Date: Mon, 7 Oct 2024 14:13:48 +0530 Subject: [PATCH 1/2] Create sorted_linked_list.py Python code to sort the numbers using linked list algorithm in data structures --- .../linked_list/sorted_linked_list.py | 46 +++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 data_structures/linked_list/sorted_linked_list.py diff --git a/data_structures/linked_list/sorted_linked_list.py b/data_structures/linked_list/sorted_linked_list.py new file mode 100644 index 000000000000..15bbcea72dec --- /dev/null +++ b/data_structures/linked_list/sorted_linked_list.py @@ -0,0 +1,46 @@ +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) + # 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.") +# Display the sorted linked list +print("Sorted Linked List:") +sll.display() From 65cd6716f53448e03611994def9d920bbb8353dc Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 7 Oct 2024 08:54:39 +0000 Subject: [PATCH 2/2] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- data_structures/linked_list/sorted_linked_list.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/data_structures/linked_list/sorted_linked_list.py b/data_structures/linked_list/sorted_linked_list.py index 15bbcea72dec..0ad405cccd41 100644 --- a/data_structures/linked_list/sorted_linked_list.py +++ b/data_structures/linked_list/sorted_linked_list.py @@ -3,6 +3,7 @@ def __init__(self, data): self.data = data self.next = None + class SortedLinkedList: def __init__(self): self.head = None @@ -28,19 +29,20 @@ def display(self): 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': + 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.") + print("Please enter a valid number or 'q' to quit.") # Display the sorted linked list print("Sorted Linked List:") sll.display()