Chapter 6: Linked lists
Linked list problems are rarely about clever algorithms. They test pointer manipulation and edge-case handling: whether you can rewire next pointers without losing part of the list, and whether your code still works when the list is empty, has one node, or has the operation land on its head. The good news is that a few primitives cover almost everything. Dummy head nodes, fast and slow pointers, and in-place reversal, plus merging and digit-by-digit arithmetic, combine in twos and threes to solve nearly every linked list problem you will meet.
Concepts from other sections (2)
This section uses these ideas. Each has a short definition and a link to the section that explains it in full. Tap a dotted term in the text to preview it.
- Fast and slow pointers: two pointers that move through a sequence at different speeds, usually one and two steps; the slow one is at the midpoint when the fast one reaches the end, and the two meet if the sequence contains a cycle. Read more: Chapter 4: Two pointers and sliding window
- Two pointers: a technique that walks two indices through an array, from opposite ends or in the same direction, so that each move provably skips only candidates that cannot be the answer; on sorted or monotonic data it turns an pair search into an sweep. Read more: Chapter 4: Two pointers and sliding window
This chapter presents each primitive with a template, closes with an edge-case checklist to run against any solution, and then walks through a recursive solution to Add Two Numbers.
Learning objectives
- Use a dummy head node to remove special cases when the head can change.
- Find the middle of a list, detect a cycle, and locate the cycle’s start with fast and slow pointers.
- Reverse a whole list and a sublist in place.
- Merge sorted lists, and add numbers stored as digit lists with correct carry handling.
Before you begin
Section titled “Before you begin”The fast and slow pointer idea was introduced in Two pointers and sliding window. You should be comfortable with references (or pointers) and with recursion. The doubly linked list behind an LRU cache is built in full in the intrusive list and LRU in this book’s systems chapter.
Dummy head nodes
Section titled “Dummy head nodes”Any problem where the head might change (insertion, deletion, merging) gets simpler with a dummy node in front of the head. Every real node then has a predecessor, so there is no “if head is null” or “if we are deleting the head” branch, and the answer is simply dummy.next:
dummy = ListNode(0)dummy.next = headprev = dummy# ... operations ...return dummy.nextRemove Nth Node From End of List, Merge Two Sorted Lists, and Partition List all use it.
Fast and slow pointers
Section titled “Fast and slow pointers”For cycle detection, finding the middle, or finding the th node from the end, move two pointers at different speeds. Floyd’s algorithm detects a cycle when the pointers meet, and then finds the cycle’s start: reset one pointer to the head and advance both one step at a time; they meet again exactly at the start of the cycle.
slow, fast = head, headwhile fast and fast.next: slow = slow.next fast = fast.next.next if slow == fast: # Cycle detected. Find start: slow = head while slow != fast: slow = slow.next fast = fast.next return slow # cycle startWithout a cycle, the same loop leaves slow at the midpoint when fast runs off the end:
slow, fast = head, headwhile fast and fast.next: slow = slow.next fast = fast.next.next# slow is at midpointLinked List Cycle, Linked List Cycle II, Middle of the Linked List, and Palindrome Linked List are the standard problems. For the th node from the end, start fast nodes ahead and move both at the same speed.
In-place reversal
Section titled “In-place reversal”Reversing a list walks it once, pointing each node back at its predecessor. Save next before overwriting it, or the rest of the list is lost:
prev = Nonecurr = headwhile curr: nxt = curr.next curr.next = prev prev = curr curr = nxtreturn prev # new headReversing only the nodes between positions and combines reversal with a dummy head. Walk pre to the node before position ; then repeatedly take the node after tail and move it to the front of the sublist:
dummy = ListNode(0, head)pre = dummyfor _ in range(m - 1): pre = pre.nexttail = pre.nextfor _ in range(n - m): tmp = pre.next pre.next = tail.next tail.next = tail.next.next pre.next.next = tmpReverse Linked List, Reverse Linked List II, and Reverse Nodes in k-Group use these templates.
Merging sorted lists
Section titled “Merging sorted lists”To combine two sorted lists, repeatedly attach the smaller head to a growing result behind a dummy node, and attach whatever remains when one list runs out:
dummy = ListNode(0)curr = dummywhile l1 and l2: if l1.val <= l2.val: curr.next = l1 l1 = l1.next else: curr.next = l2 l2 = l2.next curr = curr.nextcurr.next = l1 or l2return dummy.nextFor lists, keep the current heads in a min-heap of size , for over total nodes. Merge Two Sorted Lists and Merge k Sorted Lists are the classic problems.
Digit-by-digit arithmetic
Section titled “Digit-by-digit arithmetic”When numbers are stored as linked lists of digits in reverse order, addition is the grade-school algorithm. Keep going while either list has digits or a carry remains, which handles lists of unequal length and a final carry in one condition:
dummy = ListNode(0)curr = dummycarry = 0while l1 or l2 or carry: val = carry if l1: val += l1.val; l1 = l1.next if l2: val += l2.val; l2 = l2.next carry, digit = divmod(val, 10) curr.next = ListNode(digit) curr = curr.nextreturn dummy.nextAdd Two Numbers and Add Two Numbers II (digits in forward order, usually solved by reversing first or using stacks) are the standard problems. The two things to manage carefully are the carry and unequal lengths.
An edge-case checklist
Section titled “An edge-case checklist”Run every linked list solution against these inputs before you trust it:
- an empty list (
headis null) - a single node
- two nodes
- odd and even lengths
- a list with a cycle
- an operation on the head node
- an operation on the tail node
Interviewers favor different variants. As a rough guide:
| Company | Favorite variant | Difficulty |
|---|---|---|
| Reverse Nodes in k-Group | Hard | |
| Meta | Merge k Sorted Lists | Medium-Hard |
| Amazon | LRU cache (doubly linked list + hash map) | Medium |
| Apple | in-place operations (memory) | Medium |
| Palantir | custom linked structures | Medium |
Key terms
Section titled “Key terms”- Dummy head node: a placeholder node placed before a linked list’s real head so that every real node has a predecessor; insertions, deletions, and merges then need no special case for the head, and the result is
dummy.next. - Floyd’s cycle detection / tortoise and hare: detecting a cycle in a linked list by advancing one pointer one step and another two steps until they meet, then finding the cycle’s start by restarting one pointer at the head and advancing both one step at a time until they meet again.
- In-place list reversal: reversing a linked list in one pass with extra space by pointing each node’s
nextback at its predecessor, saving the originalnextfirst so the rest of the list is not lost. - k-way merge: merging sorted lists by keeping their current heads in a min-heap of size and repeatedly taking the smallest, in time for elements in total.
In this chapter
Section titled “In this chapter”- Add Two Numbers: a recursive version of digit-by-digit addition. The
helperfunction adds the current digits and the carry, appends a node forsum % 10to the tail, remembers the head on the first call, and recurses on bothnextpointers with the new carry, treating a missing node as . It stops when the carry is zero and both lists are exhausted. The file is a solution function for Node (it begins with anodeshebang and prints its intermediate values withconsole.log); add a driver that definesListNodeand builds the input lists to run it, or paste it into an online judge.