How to take input in Linked List in Python
# Define a Node class
class Node:
def __init__(self, value, next=None):
self.value = value
self.next = next
# Define a LinkedList class
class LinkedList:
def __init__(self, head=None):
self.head = head
def append(self, value):
# Create a new node
new_node = Node(value)
# Set the current node to the head
current = self.head
# Find the last node in the list
if current:
while current.next:
current = current.next
current.next = new_node
else:
self.head = new_node
# Test the LinkedList class
linked_list = LinkedList()
# Read in the values from the user
while True:
value = input('Enter a value (enter "done" to finish): ')
if value == 'done':
break
linked_list.append(int(value))
# Print the values of the nodes
node = linked_list.head
while node:
print(node.value)
node = node.next
0 Comments
if you are not getting it then ask i am glad to help