Skip to content

Commit 84f16ff

Browse files
committed
ArrayList vs LinkedList in Java
Signed-off-by: https://github.com/Someshdiwan <[email protected]>
1 parent dfd852b commit 84f16ff

File tree

1 file changed

+80
-0
lines changed

1 file changed

+80
-0
lines changed
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
ArrayList vs LinkedList in Java:
2+
3+
## 1. Core Idea
4+
5+
- Both `ArrayList` and `LinkedList` are implementations of the `List` interface.
6+
- They differ in **internal data structures** and **performance characteristics**.
7+
- Choosing between them depends on your use case: random access vs frequent insertions/deletions.
8+
9+
---
10+
11+
## 2. Internal Working
12+
13+
**ArrayList**
14+
- Backed by a **dynamic array** (resizable).
15+
- Elements are stored **contiguously in memory**.
16+
- Access by index is **O(1)**.
17+
- Insertions/removals in the middle require **shifting elements** → **O(n)**.
18+
19+
**LinkedList**
20+
- Backed by a **doubly linked list**.
21+
- Each node stores data + references to `next` and `prev`.
22+
- Access by index is **O(n)** (traversal needed).
23+
- Insertions/removals (once position is found) are **O(1)**, since only links are updated.
24+
25+
---
26+
27+
## 3. Performance Comparison
28+
29+
| Operation | ArrayList | LinkedList |
30+
|---------------------------|-----------------------------|-------------------------------------|
31+
| Access by Index | O(1) → Fast | O(n) → Slow (traverse list) |
32+
| Insert/Delete at End | Amortized O(1) | O(1) |
33+
| Insert/Delete in Middle | O(n) (shift elements) | O(1) (if node is known) |
34+
| Memory Usage | More efficient (contiguous)| Higher (extra pointers per node) |
35+
| Iteration | Very fast | Fast, but slightly more overhead |
36+
37+
---
38+
39+
## 4. Memory Considerations
40+
41+
- **ArrayList**: Efficient in memory usage, since it stores elements in a single contiguous block.
42+
- **LinkedList**: Consumes more memory because each node stores:
43+
- The element itself.
44+
- A reference to the previous node.
45+
- A reference to the next node.
46+
47+
---
48+
49+
## 5. Best Use Cases
50+
51+
**ArrayList**
52+
- Frequent **random access** by index.
53+
- Iteration-heavy tasks (traversing all elements).
54+
- Adding/removing mainly at the **end**.
55+
- Example: Displaying a product catalog on a webpage where items are fetched by position.
56+
57+
**LinkedList**
58+
- Frequent **insertions/deletions in the middle**.
59+
- Implementation of **stacks, queues, or deques**.
60+
- Undo/Redo operations (history management).
61+
- Example: Storing edit history in a text editor.
62+
63+
---
64+
65+
## 6. Practical Scenario: Music Player Playlist
66+
67+
- **ArrayList** is preferred:
68+
- Fast random access (`play 5th song` → O(1)).
69+
- Efficient sequential iteration (playing songs in order).
70+
- Efficient adding/removing at the end (adding new songs).
71+
- While reordering (middle insertions/deletions) may occur, it’s less frequent and the performance trade-off is
72+
negligible compared to the benefits.
73+
74+
---
75+
76+
## 7. Quick Recap
77+
78+
- Use **ArrayList** → When you need **fast random access** and mostly add/remove at the end.
79+
- Use **LinkedList** → When you need **fast insertions/removals in the middle** and don’t care about random access performance.
80+
- Both implement `List`, but the **choice depends on workload pattern**.

0 commit comments

Comments
 (0)