Linked Lists

📖 Introduction

When I first encountered linked lists, they seemed like a solution in search of a problem. Why use pointers when arrays work fine? The answer became clear when I started working with dynamic data—undo systems, browser history, task schedulers—where frequent insertions and deletions matter more than random access.

Linked lists taught me to think about memory differently: not as contiguous blocks, but as connected nodes scattered throughout the heap. Mastering pointer manipulation here laid the foundation for understanding trees, graphs, and many system-level data structures.


🎯 What Is a Linked List?

A linked list is a linear data structure where elements are connected by pointers rather than stored contiguously.

Node Structure

from typing import Optional
from dataclasses import dataclass

@dataclass
class ListNode:
    """Singly linked list node."""
    val: int
    next: Optional['ListNode'] = None


@dataclass
class DoublyListNode:
    """Doubly linked list node."""
    val: int
    prev: Optional['DoublyListNode'] = None
    next: Optional['DoublyListNode'] = None

Visual Representation

spinner
spinner

📊 Array vs Linked List

Operation
Array
Linked List

Access by index

O(1)

O(n)

Search

O(n)

O(n)

Insert at beginning

O(n)

O(1)

Insert at end

O(1)*

O(n) or O(1)†

Insert in middle

O(n)

O(1)‡

Delete at beginning

O(n)

O(1)

Delete at end

O(1)

O(n) or O(1)†

Memory

Contiguous, cache-friendly

Scattered, pointer overhead

*Amortized for dynamic arrays †O(1) if tail pointer maintained ‡After finding position


🔧 Implementing Singly Linked List


🔧 Implementing Doubly Linked List


🏃 Fast and Slow Pointers

Floyd's cycle detection algorithm uses two pointers moving at different speeds.

Detecting Cycles

spinner

Finding Middle Element

Nth Node From End


🔄 Common Linked List Operations

Reversing a Linked List

Merging Two Sorted Lists

Checking Palindrome


🎯 Advanced Linked List Problems

Add Two Numbers

Intersection of Two Lists

Flatten Multilevel List

Copy List with Random Pointer


📝 Practice Exercises

Exercise 1: Reorder List

chevron-rightSolutionhashtag

Exercise 2: Rotate List

chevron-rightSolutionhashtag

Exercise 3: Partition List

chevron-rightSolutionhashtag

🔑 Key Takeaways

  1. Linked lists excel at O(1) insertions/deletions with known positions

  2. Dummy nodes simplify edge cases (empty list, single node)

  3. Two pointers solve many problems efficiently

  4. Fast/slow pointers detect cycles and find midpoints

  5. Reversing is a fundamental operation—know both iterative and recursive

  6. Draw diagrams when manipulating pointers—it prevents errors


🚀 What's Next?

In the next article, we'll explore stacks and queues:

  • LIFO and FIFO principles

  • Implementation using arrays and linked lists

  • Monotonic stacks

  • Priority queues with heaps

Next: Article 6 - Stacks and Queues


📚 References

  • CLRS Chapter 10: Elementary Data Structures

  • LeetCode Linked List problems

  • "Cracking the Coding Interview" - Linked Lists chapter

Last updated