A singly linked list is a chain of nodes.
Each node stores a value and a pointer to the next node.
head -> [12 | next] -> [24 | next] -> [36 | null]
Why linked lists exist
Arrays store values next to each other in memory. Linked lists connect values through references.
That means a linked list can insert or remove nodes without shifting every later value, but it cannot jump directly to index 20.
Core operations
- Traverse from
head - Insert at the beginning
- Insert after a known node
- Delete after finding a node
- Search by walking node by node
Traversal
function printList(head) {
let current = head;
while (current !== null) {
console.log(current.value);
current = current.next;
}
}
Common mistakes
- Losing the rest of the list while changing links
- Forgetting to update
head - Not handling an empty list
- Infinite loops caused by accidental cycles
Complexity
- Access by index:
O(n) - Search:
O(n) - Insert at head:
O(1) - Delete known next node:
O(1)
Practice links
Use the visualizer below to move through a linked list one node at a time.
Interactive walkthrough
Linked List Traversal
Move node by node through a singly linked list.