From 71850bd043a0065bb4868d176ed37ea72badc3bd Mon Sep 17 00:00:00 2001 From: Sarvesh Tiwari Date: Tue, 15 Oct 2024 13:04:41 +0530 Subject: [PATCH 1/2] Added a code which converts an array to a Linked List!! --- data_structures/linked_list/arr_to_ll.py | 55 ++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 data_structures/linked_list/arr_to_ll.py diff --git a/data_structures/linked_list/arr_to_ll.py b/data_structures/linked_list/arr_to_ll.py new file mode 100644 index 000000000000..6a9c4284c2af --- /dev/null +++ b/data_structures/linked_list/arr_to_ll.py @@ -0,0 +1,55 @@ +class Node: + """A node in a singly linked list.""" + + def __init__(self, data: int = 0): + self.data = data # The value of the node + self.next = None # Pointer to the next node + + +def array_to_linked_list(arr: list[int]) -> Node: + """ + Convert an array into a linked list. + + :param arr: List of integers to convert into a linked list. + :return: The head of the linked list. + + >>> array_to_linked_list([1, 2, 3]) + Node(data=1, next=Node(data=2, next=Node(data=3, next=None))) + >>> array_to_linked_list([]) + >>> array_to_linked_list([4, 5, 6]) + Node(data=4, next=Node(data=5, next=Node(data=6, next=None))) + """ + if arr is None: # Check for empty array + return None + + # Create the head of the linked list + head = Node(arr[0]) + n = head # Pointer to the current node + + # Loop through the array and construct the linked list + for data in arr[1:]: + n.next = Node(data) # Create a new node and link it + n = n.next # Move to the next node + + return head # Return the head of the linked list + + +# Example usage +if __name__ == "__main__": + # Input: a list of integers + input_array = list(map(int, input("Enter array elements separated by space: ").strip().split())) + linked_list_head = array_to_linked_list(input_array) + + + # Function to print the linked list for demonstration + def print_linked_list(head: Node): + """Print the linked list.""" + n = head + while n is not None: + print(n.data, end=" -> ") + n = n.next + print("None") + + + # Print the resulting linked list + print_linked_list(linked_list_head) From 739585c635649908be5ab654d88b654e55fd399b Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 15 Oct 2024 07:38:57 +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/arr_to_ll.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/data_structures/linked_list/arr_to_ll.py b/data_structures/linked_list/arr_to_ll.py index 6a9c4284c2af..2e386319b07b 100644 --- a/data_structures/linked_list/arr_to_ll.py +++ b/data_structures/linked_list/arr_to_ll.py @@ -37,10 +37,11 @@ def array_to_linked_list(arr: list[int]) -> Node: # Example usage if __name__ == "__main__": # Input: a list of integers - input_array = list(map(int, input("Enter array elements separated by space: ").strip().split())) + input_array = list( + map(int, input("Enter array elements separated by space: ").strip().split()) + ) linked_list_head = array_to_linked_list(input_array) - # Function to print the linked list for demonstration def print_linked_list(head: Node): """Print the linked list.""" @@ -50,6 +51,5 @@ def print_linked_list(head: Node): n = n.next print("None") - # Print the resulting linked list print_linked_list(linked_list_head)