Skip to content

Commit 43202ca

Browse files
committed
feat: Both ArrayDeque and LinkedList implement the Deque (double-ended queue) interface — meaning you can add or
remove elements from both ends. But they behave quite differently internally. Signed-off-by: https://github.com/Someshdiwan <[email protected]>
1 parent 6efed29 commit 43202ca

File tree

1 file changed

+50
-0
lines changed

1 file changed

+50
-0
lines changed
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
Both ArrayDeque and LinkedList implement the Deque (double-ended queue) interface — meaning you can add or
2+
remove elements from both ends. But they behave quite differently internally.
3+
4+
5+
6+
Internal structure
7+
• ArrayDeque: Backed by a resizable circular array.
8+
• LinkedList: Backed by a doubly linked list (each node has pointers to next and previous).
9+
10+
11+
12+
Performance comparison
13+
14+
Operation Type ArrayDeque LinkedList Reason
15+
Add/remove at head or tail O(1) amortized O(1) Both are efficient for queue/deque behavior
16+
Add/remove at middle ❌ O(n) ✅ O(n) Both need traversal — LinkedList can move by pointers, but still linear
17+
Random access (like get(index)) ❌ O(n) ❌ O(n) Neither is good for index-based access
18+
Iteration ✅ Faster ⚪ Slower ArrayDeque’s array-based iteration is cache-friendly
19+
Memory overhead ✅ Lower ❌ Higher LinkedList stores two extra node pointers per element
20+
Allows null? ❌ No ✅ Yes ArrayDeque forbids nulls for sentinel logic reasons
21+
22+
23+
24+
When to use which
25+
• Use ArrayDeque for:
26+
- Queue, stack, or general double-ended operations.
27+
- High-performance iteration.
28+
- Lower memory footprint.
29+
- You don’t need null elements.
30+
31+
• Use LinkedList for:
32+
- Frequent insertions/deletions in the middle (though still O(n)).
33+
- When List behavior (like get(int index)) is also needed.
34+
- When you specifically need null elements.
35+
36+
37+
38+
Verdict:
39+
40+
For queues, stacks, or deques,
41+
→ ArrayDeque is almost always the better choice.
42+
43+
For list-like access where you occasionally modify middle elements,
44+
→ LinkedList might be acceptable, but rarely optimal compared to ArrayList.
45+
46+
47+
48+
In short:
49+
Use ArrayDeque unless you have a specific reason not to — it’s faster, leaner,
50+
and better aligned with how modern CPUs like to cache memory.

0 commit comments

Comments
 (0)