Step 08 Linked-List — Fundamentals (Single LL)
Introduction to Singly LinkedList
A linked list stores data in nodes scattered across memory, chained together by pointers, instead of in one contiguous block like an array. This representation trades instant random access for cheap insertions and deletions anywhere in the chain. Almost every linked-list problem in this module reduces to a small set of pointer moves, so this lesson builds the mental model first: what a node is, what head means, and how to walk the list without losing it.
Why linked lists exist
Arrays have two structural annoyances: their size is fixed at creation (or costly to grow), and inserting or deleting in the middle means shifting every element after that point — O(n) data movement. A linked list pays for neither: nodes are allocated one at a time wherever memory is free, and inserting or deleting is just rewiring a couple of pointers.
The price is access. An array gives you arr[i] in O(1) because elements sit at predictable offsets. A linked list has no offsets — to reach the 50th node you must follow 50 next pointers, so access and search are O(n). Keep this trade-off in mind; it explains every complexity figure in this module.
Anatomy of a node
Each node holds two things: the data value, and next — a reference to the following node, or None if this is the last node. The list itself is represented by a single reference, head, pointing at the first node. An empty list is simply head = None. The last node (whose next is None) is called the tail.
Crucially, the chain is the only path through the data. If you ever overwrite a next pointer before saving where it pointed, everything after it becomes unreachable — 'losing the list' is the classic linked-list bug, and most techniques in this module exist to avoid it.
class Node:
def __init__(self, data, next=None):
self.data = data
self.next = next
def array_to_list(arr):
if not arr:
return None
head = Node(arr[0])
tail = head
for x in arr[1:]:
tail.next = Node(x)
tail = tail.next
return headTraversal: walking without losing the list
Every read operation — printing, counting length, searching for a value — is the same walk: start a temporary pointer at head, use it, then advance temp = temp.next until temp is None.
Always walk with a separate temp pointer, never with head itself. If you advance head, you have thrown away the start of the list and can never get back. Length counting and linear search are simply this walk with a counter or a comparison added, both O(n) time and O(1) extra space.
def traverse(head):
temp = head
while temp is not None:
print(temp.data, end=' -> ')
temp = temp.next
print('None')
def length(head):
count = 0
temp = head
while temp is not None:
count += 1
temp = temp.next
return countComplexity summary and common mistakes
Access by position: O(n). Search: O(n). Insert/delete at the head: O(1). Insert/delete at the tail or at position k: O(n) because you must walk there first (the rewiring itself is O(1)). Space per node is O(1) extra for the pointer.
Common mistakes: (1) moving head during traversal instead of a temp pointer; (2) forgetting the empty-list case — almost every operation must ask 'what if head is None?' first; (3) writing temp.next.next without checking temp.next exists, which crashes on the last node; (4) assuming a tail pointer exists — a plain singly linked list keeps only head unless you maintain a tail reference yourself, as array_to_list above does temporarily during construction.
Key points
- A node = data + next pointer; the list is just the head reference, and the last node's next is None.
- Arrays win on random access O(1); linked lists win on insert/delete at a known position O(1) rewiring.
- Never traverse with head itself — use a temp pointer or you lose the list.
- Reaching any position costs O(n); only work at the head is truly O(1).
- Every operation must first handle head = None (empty list).