Linked Lists In PHP
Linked lists connect nodes through references rather than storing items in one contiguous logical sequence.
Why This Matters
They teach pointer-style structure and constant-time insertion when a node is already known, but PHP arrays or SPL containers are usually more practical for application code.
Working Model
A singly linked node points forward; a doubly linked node points both ways. Finding an index remains linear, and each PHP object adds substantial memory overhead.
Practical Rules
- Use linked structures when node insertion/removal behavior is the actual requirement.
- Prefer SPL implementations over custom nodes in production.
- Track head, tail, and size invariants.
- Handle cycles explicitly.
- Choose arrays for ordinary iteration and keyed lookup.
Failure Modes
- Claiming all insertions are constant time when the node must first be found.
- Leaking nodes through accidental cycles.
- Using custom lists as a generic performance optimization.
- Ignoring PHP object memory cost.
Verification
- Test empty, one-node, head, tail, and middle operations.
- Validate forward and backward links.
- Detect cycles in debug tests.
- Benchmark against arrays for the real workload.
What You Should Be Able To Do
After this lesson, you should be able to explain linked-list invariants and why they are less common than arrays in everyday PHP, choose a suitable approach for a real PHP project, and verify the result instead of relying on assumptions.
Practice
Practice: Insert After A Node
Describe insertion after a known node in a singly linked list.
Your answer must:
- state the intended outcome;
- show the commands, data flow, or implementation shape;
- identify at least one unsafe alternative;
- explain how the result will be verified.
Show solution
Point the new node to the old next node, then point the known node to the new node. Update the tail if the known node was last.
The important part is not memorising one command or vendor screen. The solution makes the invariant, failure behavior, and verification evidence explicit.
Practice: Detect A Cycle
Design cycle detection without storing every node.
Your answer must:
- state the intended outcome;
- show the commands, data flow, or implementation shape;
- identify at least one unsafe alternative;
- explain how the result will be verified.
Show solution
Use slow and fast references; advance one by one step and one by two. A meeting indicates a cycle, while a null fast pointer proves termination.
The important part is not memorising one command or vendor screen. The solution makes the invariant, failure behavior, and verification evidence explicit.
Practice: Choose Array Or List
Choose structures for API rows, an LRU cache order, and ID lookup.
Your answer must:
- state the intended outcome;
- show the commands, data flow, or implementation shape;
- identify at least one unsafe alternative;
- explain how the result will be verified.
Show solution
Use arrays for API rows, a purpose-built linked map or library for LRU ordering, and a keyed map for ID lookup. Avoid a custom list unless it removes real complexity.
The important part is not memorising one command or vendor screen. The solution makes the invariant, failure behavior, and verification evidence explicit.