Sobes.tech
Junior — Middle

What types of data or objects can be stored in a singly linked list?

sobes.tech AI

Answer from AI

In a singly linked list in Python, you can store objects of any type — this is due to the language's dynamic typing. Usually, list nodes store references to objects rather than the objects themselves, so you can store:

  • Primitive types: int, float, str, bool
  • Complex objects: lists, dictionaries, custom classes
  • Functions, lambda expressions

Example of a node in a singly linked list that stores any object:

class Node:
    def __init__(self, data):
        self.data = data
        self.next = None

# Example usage
node1 = Node(42)          # number
node2 = Node("text")    # string
node3 = Node([1, 2, 3])   # list