Linked Lists

November 18, 2025
#linked-list #data-structures #algorithms #interviewing

Introduction

A linked list is a linear data structure made of nodes where each node stores a value and a reference (a “link”) to the next node. Unlike arrays, linked lists do not store elements contiguously in memory. This gives them different performance characteristics and trade-offs.

If you are new to time and space complexity, start with our guide to Big-O Notation. You may also want to review Arrays to understand how linked lists compare.

Why use a linked list?

Choose a linked list when:

  1. You need frequent insertions or deletions near the head or at known nodes: O(1) when you already have a reference to the node.
  2. Memory reallocation costs of resizing arrays are undesirable.
  3. You are building other structures (e.g., stacks, queues, adjacency lists) where linked lists are a good fit.

Avoid a linked list when:

  • You need fast random access by index. Arrays provide O(1) index access; linked lists require O(n) traversal.
  • Cache locality matters a lot; arrays are typically more cache-friendly due to contiguous memory.

Flavors of linked lists

  1. Singly linked list: Each node has value and next.
  2. Doubly linked list (DLL): Each node has value, prev, and next for bidirectional traversal.
  3. Circular linked list: The tail’s next points back to the head (and for DLL, head’s prev points to tail), forming a loop.

This tutorial focuses on the singly linked list, then discusses variations.

Core operations and complexity

  • Access kth element by index: O(n) — must traverse from head.
  • Search by value: O(n) — scan nodes until found.
  • Insert at head: O(1) — create a node and point it to the old head.
  • Insert after a given node: O(1) — if you have the node reference.
  • Delete head: O(1) — move head to head.next.
  • Delete after a given node: O(1) — if you have the previous node reference.
  • Insert at tail: O(1) if you maintain a tail pointer; otherwise O(n) to find the tail.

Singly linked list implementation

Below are minimal singly linked list implementations providing: insert at head, search, delete (by value), and iteration/printing.

Using a linked list for a queue

Queues are often implemented using linked lists to achieve O(1) enqueue and dequeue with a head and tail pointer.

Variations and extensions

  • Doubly linked list: Add prev to each node. Pros: O(1) delete with node reference and easier bidirectional traversal. Cons: more memory, more pointer updates.
  • Circular linked list: Useful for round-robin scheduling; the last node links to the first.
  • Sentinel (dummy) nodes: Simplify edge cases by using a dummy head and/or tail.

Pitfalls and edge cases

  • Forgetting to update tail when deleting the last node.
  • Memory management: In manual-memory languages, ensure nodes are freed to avoid leaks.
  • Infinite loops in circular lists: Ensure termination conditions are correct.
  • Off-by-one errors when inserting/deleting near the head or tail.

When to prefer arrays over linked lists

  • You need frequent random reads by index.
  • Data fits comfortably in memory and resizing costs are acceptable.
  • You benefit from CPU cache locality and vectorized operations.

For sorted arrays where you frequently search, consider Binary Search.

Practice prompts

  1. Implement a popHead() method that removes and returns the head in O(1) time.
  2. Implement a doubly linked list with addFirst, addLast, remove(Node), and find.
  3. Detect a cycle in a linked list using Floyd’s Tortoise and Hare algorithm. Return the node where the cycle begins.
  4. Merge two sorted singly linked lists into one sorted list in O(n + m) time.
  5. Reverse a singly linked list iteratively, then recursively. Analyze time/space complexity.

Complexity summary

  • Time:
    • Search: O(n)
    • Insert at head/tail (with tail pointer): O(1)
    • Delete at head: O(1); delete by value with traversal: O(n)
  • Space:
    • O(n) for node storage; each node stores data plus pointer(s)
Thanks for visiting
We are actively updating content to this site. Thanks for visiting! Please bookmark this page and visit again soon.
Sponsor