Singly & Doubly Linked Lists
Kick off Module 3 by exploring non-contiguous memory allocations, pointer manipulation mechanics, and classic traversal algorithms.
1. Node Structure & Memory Mechanics
Unlike arrays that require continuous blocks of RAM, a linked list consists of independent nodes scattered across memory. Each node stores data along with one or more references (pointers) to neighboring nodes. Because elements are not indexed adjacently, random access requires $O(n)$ linear traversal.
Singly vs. Doubly Linked Lists: Singly linked list nodes contain a single `next` pointer pointing forward. Doubly linked lists add a `prev` pointer, enabling bidirectional traversal and $O(1)$ deletions given a direct node reference at the cost of extra memory overhead.
2. Insertions, Deletions & Finding Midpoints
Inserting or deleting nodes at the head of a linked list takes $O(1)$ time because you only need to reroute pointer references. Finding the midpoint of a list without knowing its length ahead of time is efficiently achieved using the fast and slow pointer technique.
3. Cycle Detection (Floyd's Tortoise and Hare Algorithm)
To determine if a linked list contains a circular loop, Floyd's cycle-finding algorithm deploys two pointers traversing at different speeds: a slow pointer moving one step at a time and a fast pointer moving two steps. If the pointers meet, a cycle is guaranteed to exist.
4. Homework & Quiz Overview
To lock in your mastery, complete the implementation exercises for reversing a linked list and detecting/removing cycles using Floyd's algorithm.
Quick Check: Test Your Intuition
What is the time complexity of reversing a singly linked list iteratively in-place?