Linked List
ELI5 — The Vibe Check
A linked list is like a treasure hunt where each clue tells you where the next clue is. Each item (node) holds a value AND a pointer to the next item. Unlike arrays, items are not side-by-side in memory — they are scattered around, connected by pointers. Great for inserting items in the middle, terrible for random access.
Real Talk
A linked list is a linear data structure where each node contains data and a reference (pointer) to the next node. Unlike arrays, nodes are not stored contiguously in memory. Linked lists provide O(1) insertion and deletion at the head, but O(n) access by index. Doubly linked lists also store a pointer to the previous node, enabling traversal in both directions.
Show Me The Code
// Implementing a simple linked list in JS:
class Node {
constructor(value) {
this.value = value;
this.next = null;
}
}
class LinkedList {
constructor() { this.head = null; }
prepend(value) {
const node = new Node(value);
node.next = this.head;
this.head = node; // O(1)
}
}
// head → [1] → [2] → [3] → null
When You'll Hear This
"Linked lists are great for queues where you insert and delete from both ends." / "Arrays are usually better unless you need O(1) insertion in the middle."
Related Terms
Array
An array is a list of things in order, like a numbered row of boxes. Box 0 holds the first item, box 1 holds the second, and so on.
Data Structure
A data structure is a way of organizing information in your code so it is easy to use and fast to access.
Queue
A queue is like a line at a coffee shop — first come, first served. The first person to get in line is the first to get their coffee.
Stack
A stack is a pile of things where you can only add to the top and take from the top — like a stack of plates.