Linked List
Also Known As
singly linked list
doubly linked list
TL;DR
A linear data structure where elements (nodes) store a value and a pointer to the next node, enabling O(1) insertion and deletion at known positions.
Explanation
Unlike arrays, linked lists do not store elements contiguously in memory. Singly linked lists have one pointer (next); doubly linked lists have two (next and prev); circular lists loop back to the head. The trade-off: O(1) insert/delete at a known node, but O(n) access by index. PHP's SplDoublyLinkedList implements this. Linked lists underlie many higher-level structures: stacks, queues, and LRU caches.
Diagram
flowchart LR
subgraph Singly_Linked[Singly Linked]
H[Head] --> N1["data=A<br/>next →"] --> N2["data=B<br/>next →"] --> N3[data=C<br/>next=null]
end
subgraph Doubly_Linked[Doubly Linked]
D1["← prev<br/>data=A<br/>next →"] <--> D2["← prev<br/>data=B<br/>next →"] <--> D3["← prev<br/>data=C<br/>next=null"]
end
subgraph Complexity
OPS[Insert at known node O 1<br/>Delete at known node O 1<br/>Access by index O n]
end
style H fill:#6e40c9,color:#fff
style N3 fill:#238636,color:#fff
Common Misconception
✗ Linked lists are always slower than arrays — for frequent mid-list insertions/deletions at known positions they are O(1) vs O(n) for arrays.
Why It Matters
Understanding linked lists is foundational for implementing queues, LRU caches, and understanding why PHP's SplDoublyLinkedList outperforms array_shift() on large datasets.
Common Mistakes
- Using array_shift() on large arrays in PHP — O(n) reindex; use SplQueue (doubly linked) for O(1) dequeue.
- Not tracking the tail pointer — appending to a singly linked list is O(n) without it.
- Forgetting to update both next and prev pointers in a doubly linked list — creates broken links.
- Using a linked list where random access is needed — O(n) traversal makes it the wrong choice.
Code Examples
✗ Vulnerable
// Array used as queue — O(n) shift on large datasets:
$queue = [];
for ($i = 0; $i < 100000; $i++) array_push($queue, $i);
$next = array_shift($queue); // Reindexes 99,999 elements — O(n)
✓ Fixed
// SplQueue (doubly linked) — O(1) enqueue and dequeue:
$queue = new SplQueue();
for ($i = 0; $i < 100000; $i++) $queue->enqueue($i);
$next = $queue->dequeue(); // O(1) — no reindex
Tags
🤝 Adopt this term
£79/year · your link shown here
Added
15 Mar 2026
Edited
19 Apr 2026
Views
24
🤖 AI Guestbook educational data only
|
|
Last 30 days
Agents 0
No pings yet today
No pings yesterday
Amazonbot 6
Perplexity 5
Ahrefs 3
Unknown AI 2
Google 2
Majestic 1
Also referenced
How they use it
crawler 17
crawler_json 1
pre-tracking 1
Related categories
⚡
DEV INTEL
Tools & Severity
🟢 Low
⚙ Fix effort: Medium
⚡ Quick Fix
PHP arrays are hash maps not linked lists — SplDoublyLinkedList provides O(1) insert/delete at ends; use it when you need frequent insertions at arbitrary positions in large collections
📦 Applies To
PHP 5.3+
web
cli
🔗 Prerequisites
🔍 Detection Hints
array_unshift() on large array O(n) when SplDoublyLinkedList::unshift() O(1) would work; frequent middle insertions on large PHP arrays
Auto-detectable:
✗ No
blackfire
⚠ Related Problems
🤖 AI Agent
Confidence: Low
False Positives: High
✗ Manual fix
Fix: Medium
Context: Function
Tests: Update