linked list
A linked list is a linear data structure in which each element points to the next, so the elements form a chain rather than sitting in one contiguous block of memory. Each element, called a node, holds a value and a reference to the following node:
Because nodes are joined by references, inserting or removing an element is cheap once its position is known. Only a couple of references change, an O(1) operation. The trade-off is that reaching the element at a given index means walking the chain from the start, an O(n) operation, since there is no direct indexing the way a contiguous array allows.
Click any node in the singly linked list below to see how many references you follow from the head to reach it, then insert or delete to watch how few references change:
Variants include the singly linked list, the doubly linked list whose nodes also point backward, and the circular linked list whose last node points back to the first. Linked lists also underpin other structures, such as stacks, queues, and the adjacency lists used to represent a graph.
Related Resources
Tutorial
Linked Lists in Python: An Introduction
In this article, you'll learn what linked lists are and when to use them, such as when you want to implement queues, stacks, or graphs. You'll also learn how to use collections.deque to improve the performance of your linked lists and how to implement linked lists in your own projects.
For additional information on related topics, take a look at the following resources:
- Common Python Data Structures (Guide) (Tutorial)
- Python's collections: A Buffet of Specialized Data Types (Tutorial)
- Python's deque: Implement Efficient Queues and Stacks (Tutorial)
- Working With Linked Lists in Python (Course)
- Dictionaries and Arrays: Selecting the Ideal Data Structure (Course)
- Python Stacks, Queues, and Priority Queues in Practice (Tutorial)
- Linked Lists in Python: An Introduction (Quiz)
- Stacks and Queues: Selecting the Ideal Data Structure (Course)
- Records and Sets: Selecting the Ideal Data Structure (Course)
- Python Stacks, Queues, and Priority Queues in Practice (Quiz)