-
-
Notifications
You must be signed in to change notification settings - Fork 48.7k
Added a code which converts an array to a Linked List!! #12097
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
Changes from all commits
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,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): | ||
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. As there is no test file in this pull request nor any test function or class in the file Please provide return type hint for the function: |
||
"""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) |
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: