← Home ← Codex ← DEBT
Browse by Category
+ added · updated 7d
← Back to glossary

Linked List

Data Structures PHP 5.3+ Beginner
debt(d7/e3/b3/t5)
d7 Detectability Operational debt — how invisible misuse is to your safety net

Closest to 'only careful code review or runtime testing' (d7). The detection_hints list only Blackfire (a profiler) and note 'automated: no'. The misuse pattern — using array_shift()/array_unshift() on large arrays instead of SplDoublyLinkedList — won't be caught by a linter or compiler; it requires profiling under load or careful code review to notice the O(n) performance penalty.

e3 Effort Remediation debt — work required to fix once spotted

Closest to 'simple parameterised fix' (e3). The quick_fix points to swapping PHP arrays for SplDoublyLinkedList/SplQueue. This is more than a single-line patch — callers must be updated to use the Spl API (push/pop/unshift instead of array functions) — but it's contained within one component or data-access layer, not a cross-cutting refactor.

b3 Burden Structural debt — long-term weight of choosing wrong

Closest to 'localised tax' (b3). The choice applies to specific collections within web or CLI contexts. Once the correct structure is chosen, the rest of the codebase is unaffected. The debt is felt only in the component that handles the data structure, not system-wide.

t5 Trap Cognitive debt — how counter-intuitive correct behaviour is

Closest to 'notable trap' (t5). The misconception field states developers wrongly assume linked lists are always slower than arrays, when in fact they are O(1) for mid-list insertions/deletions vs O(n) for arrays. Conversely, common_mistakes show developers also misuse PHP arrays (treating them as linked lists) and forget pointer bookkeeping. These are documented, well-known gotchas that most developers learn after the fact, consistent with a t5 score.

About DEBT scoring →

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

Added 15 Mar 2026
Edited 19 Apr 2026
Views 52
Rate this term
No ratings yet
🤖 AI Guestbook educational data only
| |
Last 30 days
0 pings T 0 pings W 1 ping T 0 pings F 0 pings S 1 ping S 1 ping M 0 pings T 0 pings W 1 ping T 2 pings F 2 pings S 1 ping S 0 pings M 3 pings T 0 pings W 0 pings T 0 pings F 0 pings S 0 pings S 2 pings M 0 pings T 0 pings W 0 pings T 0 pings F 0 pings S 0 pings S 0 pings M 1 ping T 0 pings W
No pings yet today
PetalBot 1
Scrapy 8 Amazonbot 7 Perplexity 5 Ahrefs 5 SEMrush 3 Majestic 2 Unknown AI 2 Google 2 ChatGPT 2 Claude 1 Meta AI 1 Bing 1 PetalBot 1
crawler 36 crawler_json 3 pre-tracking 1
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


✓ schema.org compliant