A Singly Linked List is a linear data structure where each element (called a node) points to the next element in the sequence, forming a chain.
NULL
, indicating the end of the list.class Node:
def __init__(self, data):
self.data = data
self.next = None
class SinglyLinkedList:
def __init__(self):
self.head = None
def insert_at_beginning(self, data):
new_node = Node(data)
new_node.next = self.head
self.head = new_node
def print_list(self):
current = self.head
while current:
print(current.data, end=" -> ")
current = current.next
print("None")
# Usage
sll = SinglyLinkedList()
sll.insert_at_beginning(10)
sll.insert_at_beginning(20)
sll.insert_at_beginning(30)
sll.print_list() # Output: 30 -> 20 -> 10 -> None
This simple Python example creates and manipulates a singly linked list by inserting nodes at the beginning and printing the list.