Singly Linked List Basics
Linked Lists: lesson 1 of 6
Each item carries the address of the next one.
Lesson 1 of 6 · 4 min
Singly Linked List Basics
Step 1 of 9
head
cur
·
3•
∅
·
•
·
•
·
•
One node is two fields: a value and the address of the next node. A fresh node links to nothing.
The Idea
A linked list stores each value in its own node, and every node keeps the address of the next one. Nothing sits side by side in memory. You hold only the head, and the chain ends at a node pointing to None.
Real-World Example
A scavenger hunt works exactly like this. The card in your hand gives you one prize and one instruction: look behind the oak tree next. Nobody hands you the full route, and clue seven is unreachable until you have opened the six before it.
The Code
class Node:
def __init__(self, value):
self.value = value
self.next = None # link to the next node
head = Node(3)
head.next = Node(7) # 3 -> 7 -> None
print(head.value) # 3
print(head.next.value) # 7
print(head.next.next) # None marks the end
Your turn
What does this print?
class Node:
def __init__(self, v):
self.value, self.next = v, None
a = Node(1)
a.next = Node(2)
b = a.next
print(b.value, b.next)Mini quiz
1 / 3