bhylu.com

Advertisements

Linked Lists

A linked list stores a value and a reference to the next node. Traversal follows those references rather than using contiguous positional storage.

Understand the idea

This minimal singly linked list demonstrates the structure. Finding an item is linear because you walk from the head. Inserting after a known node can avoid shifting an array, but finding that node may still take linear work. Python's object overhead often makes built-in collections a better practical choice.

Where to try it

Open this example with Try in playground. The code loads as editable text and does not run until you select Run. The first run downloads Python. Each run starts fresh; temporary files created by a script are not files on your PC.

A worked example

Python

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

head = Node("Mira", Node("Dev"))
current = head
while current is not None:
    print(current.value)
    current = current.next

Read the result

Mira and Dev print in link order.

Try one change

Insert Noor between the two nodes without losing the reference to Dev.