How to take input in Linked List in Python

How to take input in Linked List  in Python

To take input for a linked list in Python, you can use a loop to read in the values and create the nodes one at a time. Here is an example of how you might do this:

# Define a Node class

class Node:

  def __init__(self, value, next=None):

    self.value = value = 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:


        current = = new_node


      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':



# Print the values of the nodes

node = linked_list.head

while node:


  node =

This approach works by reading in the values from the user using a loop and appending them to the linked list using the append method. The append method creates a new node with the given value and adds it to the end of the list.

Post a Comment