Skip to content
Data Structures & Algorithmsbeginner · 40 min

Linked Lists

Prerequisites: Arrays & Strings

Nodes connected by pointers. O(1) insert/delete given a node, but O(n) access. Interview favourites: reversal, cycle detection, and merging — all done with careful pointer bookkeeping.

Singly vs doubly linked

A singly linked list node holds a value and a `next` pointer; a doubly linked list adds `prev`. Insertion/deletion at a known node is O(1) (just relink pointers) but finding that node is O(n). Unlike arrays there is no random access and no contiguous-memory cache benefit.

The fast/slow pointer

Floyd's tortoise-and-hare uses two pointers moving at different speeds: it detects cycles (they meet), finds the middle (slow lands at the midpoint when fast reaches the end), and finds the kth-from-end node. It's the single most-tested linked-list idea.

Dummy heads

Allocating a dummy/sentinel head node removes special-casing of the first element when inserting, deleting, or merging — your loop logic stays uniform and you return `dummy.next` at the end.

Resources

Practice & test yourself

Take the quiz →