A linear data structure where elements are stored in nodes. Each node contains a data field and a reference (or 'pointer') to the next node in the sequence. Unlike arrays, linked lists do not have a fixed size and allow for efficient insertions and deletions.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
{
"status": "Initializing Linked List"
}Overview
A singly linked list is a chain of nodes, each holding a value and a pointer to the next node. The list keeps a reference to the first node, the head, and the last node points to null. Unlike an array, the nodes do not need to be next to each other in memory.
That layout makes inserting or removing a node O(1) once you have a reference to its predecessor — you only change a pointer — but reaching the k-th element requires walking k links from the head. There is no random access.
The default commands insert 10, 20 and 5 at the end, delete 20 (by pointing 10's next directly at 5), then insert 15. The final list is 10 -> 5 -> 15.
Step by step
- Insert at the end: walk to the last node (or keep a tail pointer) and set its
nextto the new node. - Insert at the head: set the new node's
nextto the current head, then make it the head. - Delete a value: walk with a
prevpointer untilcurrentholds the value, then setprev.next = current.next. - Traverse: start at the head and follow
nextuntil null.
Complexity
| Case | Cost | Why |
|---|---|---|
| Access / search | O(n) | Must walk from the head. |
| Insert / delete at head | O(1) | Only the head pointer changes. |
| Insert at tail | O(n) or O(1) | O(1) if a tail pointer is maintained. |
| Delete by value | O(n) | Finding the node dominates; unlinking is O(1). |
When to use it
- Frequent insertions and deletions at the front, such as a simple stack.
- Building blocks for hash table chains, adjacency lists and free lists in memory allocators.
- Interview classics: reversing a list, detecting cycles, merging sorted lists, finding the middle.
Common mistakes
- Deleting the head is a special case; a dummy (sentinel) head node removes it.
- Losing the rest of the list by overwriting
nextbefore saving it, especially when reversing. - Forgetting to update the tail pointer when the last node is deleted.
A dry run is the best way to check your understanding of Singly Linked List: follow each row, predict the next one, and compare. The run below takes 13 steps in total. The Line column refers to the line numbers in the code listing above.
- Input
- insert 10,insert 20,insert 5,delete 20,insert 15
- Result
- status = Operations complete, final_list = [10 -> 5 -> 15]
| Step | Line | What happens |
|---|---|---|
| 1 | 7 | status = Initializing Linked List |
| 2 | 11 | action = insert(10) |
| 3 | 19 | status = Inserted 10, head = 10 |
| 4 | 11 | action = insert(20) |
| 5 | 19 | status = Inserted 20, head = 10 |
| 6 | 11 | action = insert(5) |
| 7 | 19 | status = Inserted 5, head = 10 |
| 8 | 11 | action = delete(20) |
| 9 | 11 | action = delete(20), status = Found 20 at index 1 |
| 10 | 11 | status = Deleted 20, head = 10 |
| 11 | 11 | action = insert(15) |
| 12 | 19 | status = Inserted 15, head = 10 |
| 13 | 20 | status = Operations complete, final_list = [10 -> 5 -> 15] |
What is a Linked List?
What is the difference between an Array and a Linked List?
What are the primary operations of a Linked List?
- Insertion: Adds an element at the beginning, end, or a specific position.
- Deletion: Removes an element from the list.
- Search: Finds an element in the list.
- Traversal: Iterates through all the nodes in the list.
What is a 'node' in a linked list?
What is the 'head' of a linked list?
What is the time complexity for operations?
- Access/Search: O(n) because you may have to traverse the entire list.
- Insertion/Deletion at beginning: O(1).
- Insertion/Deletion at end: O(n) because you must traverse to the end first.