Data Structures · Visualization
Linked List: Introduction
Linked List
A linked list is a chain of separate nodes, where each node holds a value and a pointer (link) to the next node. Time: O(1) to access head/tail if tracked, O(n) to access an arbitrary node. Space: O(n).
Big-O: Lookup O(n); insert/delete at head O(1).
Linked List: Introduction
Linked ListA linked list is a chain of separate nodes, where each node holds a value and a pointer (link) to the next node. Unlike an array, the nodes do not have to sit next to each other in memory — they can be scattered anywhere, and the links are what keep them in order. The list starts at a 'head' pointer and ends when a node's next pointer is null.
How Linked List: Introduction Works
- Create a node containing a value and a 'next' pointer.
- Point the 'next' field to the following node in the chain, or to null if it is the last node.
- Keep a 'head' reference pointing to the first node so the list can always be found.
- To visit the whole list, start at head and keep following 'next' until you reach null.
Linked List: Introduction Worked Example
Input: Build a list holding 10, 20, 30
- Create node(10), node(20), node(30)
- node(10).next = node(20)
- node(20).next = node(30)
- node(30).next = null
- head = node(10)
Output: head -> 10 -> 20 -> 30 -> null
Press ▶ Run to watch it step by step.
class Node:
def __init__(self, val):
self.val = val
self.next = None
class LinkedList:
def __init__(self):
self.head = None
# example: intro on [10, 20, 30]Linked List: Introduction Time & Space Complexity
When to Use Linked List: Introduction
- Implementing stacks, queues, and other structures that need fast front insertion/removal
- Situations where the size is unknown and frequent insertions/deletions happen in the middle
- Undo history, music playlists, and browser back/forward chains
Recommended resourcesAffiliate links · may earn us a commission
Recommended for learners
Ready for the next step?
See the same ideas in action — visualize the algorithms that operate on these structures, or take a guided course.